Advertisement
aircampro

vector tuple pair tie ignore

Apr 13th, 2022
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.47 KB | None | 0 0
  1. // c++23 compiles as :- g++ prog.cc -Wall -Wextra -std=gnu++2b -pedantic-errors -fstack-protector-all
  2. //
  3. // This example shows examples of pair, tuple and vector
  4. //
  5. #include <iostream>
  6. #include <vector>
  7. #include <algorithm>
  8. #include <functional>
  9. #include <cstdlib>
  10. #include <bits/stdc++.h>
  11.  
  12. #include <string>
  13. #include <fstream>
  14. #include <regex>
  15.  
  16. void f( std::vector<std::pair<int, int>>& p, int x)
  17. {
  18. p.push_back(std::make_pair(1,x));
  19. p.push_back(std::make_pair(90,162));
  20. p.push_back(std::make_pair(11,12));
  21. p.push_back(std::make_pair(x,102));
  22. }
  23.  
  24. std::tuple<std::string, std::string> t( std::string tagname, std::string value )
  25. {
  26. return std::make_tuple( tagname, value );
  27. }
  28.  
  29. std::optional<float> oFloat = std::nullopt;
  30.  
  31. // parses string input to an int or null
  32. std::int32_t ParseStringToInt(std::string& arg)
  33. {
  34. try
  35. {
  36. return { std::stoi(arg) };
  37. }
  38. catch (...)
  39. {
  40. std::cout << "cannot convert \'" << arg << "\' to int!\n";
  41. return -1;
  42. }
  43.  
  44. }
  45.  
  46. std::int32_t get_tag_value( std::string tag_to_fetch, std::tuple<std::string, std::string> tup) {
  47. std::regex pattern(".*" + tag_to_fetch + ".*");
  48. if (std::regex_match(get<0>(tup), pattern)) {
  49. return ParseStringToInt(std::get<1>(tup));
  50. }
  51. return -1;
  52. }
  53.  
  54. int main(void)
  55. {
  56. std::vector<std::pair<int, int> > p;
  57. f(p,67);
  58.  
  59. // print the vector as it was made
  60. //
  61. for (auto x : p) {
  62. std::cout << x.first << " " << x.second << std::endl;
  63. }
  64.  
  65. // sort this in order of greatest first index value
  66. //
  67. std::sort( begin( p ), end( p ), std::greater<std::pair<int, int>>() );
  68. for (auto x : p) {
  69. std::cout << "first "<< x.first << " second " << x.second << std::endl;
  70. }
  71.  
  72. // now do the tuple
  73. //
  74. std::tuple<std::string, std::string> x = t("my_first_value", "2");
  75. std::string z;
  76. std::tie(std::ignore, z) = x;
  77. std::cout << z << std::endl;
  78. auto val = get_tag_value("my_first_value", x);
  79. std::cout << std::get<0>(x) << " tags value is " << val << std::endl;
  80.  
  81. x = t("my_second_value", "20");
  82. std::tie(std::ignore, z) = x;
  83. std::cout << z << std::endl;
  84. val = get_tag_value("my_first_value", x);
  85. std::cout << "my_first_value tags value is " << val << std::endl;
  86. val = get_tag_value("my_second_value", x);
  87. std::cout << std::get<0>(x) << " tags value is " << val << std::endl;
  88.  
  89. return 0;
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement