Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // c++23 compiles as :- g++ prog.cc -Wall -Wextra -std=gnu++2b -pedantic-errors -fstack-protector-all
- //
- // This example shows examples of pair, tuple and vector
- //
- #include <iostream>
- #include <vector>
- #include <algorithm>
- #include <functional>
- #include <cstdlib>
- #include <bits/stdc++.h>
- #include <string>
- #include <fstream>
- #include <regex>
- void f( std::vector<std::pair<int, int>>& p, int x)
- {
- p.push_back(std::make_pair(1,x));
- p.push_back(std::make_pair(90,162));
- p.push_back(std::make_pair(11,12));
- p.push_back(std::make_pair(x,102));
- }
- std::tuple<std::string, std::string> t( std::string tagname, std::string value )
- {
- return std::make_tuple( tagname, value );
- }
- std::optional<float> oFloat = std::nullopt;
- // parses string input to an int or null
- std::int32_t ParseStringToInt(std::string& arg)
- {
- try
- {
- return { std::stoi(arg) };
- }
- catch (...)
- {
- std::cout << "cannot convert \'" << arg << "\' to int!\n";
- return -1;
- }
- }
- std::int32_t get_tag_value( std::string tag_to_fetch, std::tuple<std::string, std::string> tup) {
- std::regex pattern(".*" + tag_to_fetch + ".*");
- if (std::regex_match(get<0>(tup), pattern)) {
- return ParseStringToInt(std::get<1>(tup));
- }
- return -1;
- }
- int main(void)
- {
- std::vector<std::pair<int, int> > p;
- f(p,67);
- // print the vector as it was made
- //
- for (auto x : p) {
- std::cout << x.first << " " << x.second << std::endl;
- }
- // sort this in order of greatest first index value
- //
- std::sort( begin( p ), end( p ), std::greater<std::pair<int, int>>() );
- for (auto x : p) {
- std::cout << "first "<< x.first << " second " << x.second << std::endl;
- }
- // now do the tuple
- //
- std::tuple<std::string, std::string> x = t("my_first_value", "2");
- std::string z;
- std::tie(std::ignore, z) = x;
- std::cout << z << std::endl;
- auto val = get_tag_value("my_first_value", x);
- std::cout << std::get<0>(x) << " tags value is " << val << std::endl;
- x = t("my_second_value", "20");
- std::tie(std::ignore, z) = x;
- std::cout << z << std::endl;
- val = get_tag_value("my_first_value", x);
- std::cout << "my_first_value tags value is " << val << std::endl;
- val = get_tag_value("my_second_value", x);
- std::cout << std::get<0>(x) << " tags value is " << val << std::endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement