Get field value by std::get from container

We can use std::get to fetch field value from container object.
It can help us to avoid use iteractor to access first or second element.

#include <iostream>
#include <tuple>
#include <string>
#include <map>
#include <vector>
using namespace std;

using Info = std::tuple<std::string, int, double>;
using mapper = std::map<std::string, double>;
int main()
{
    std::vector<Info> objs;
    objs.push_back( std::make_tuple( "hello", 1, 10.0 ) );
    objs.push_back( std::make_tuple( "world", 2, 20.0 ) );
    objs.push_back( std::make_tuple( "mua!", 3, 30.0 ) );
    for( auto obj: objs )
    {
        auto intValue = std::get<1>( obj ); // get the second field value in container.
        std::cout << intValue << std::endl;
    }

    mapper pers;
    pers["hello"] = 10.0;
    pers["world"] = 20.0;
    for( auto obj: pers )
    {
        auto doubleValue = std::get<0>( obj ); // get the first field value in container.
        std::cout << doubleValue << std::endl;
    }
    return 0;
}

Output:

1
2
3
hello
world
Categories: CPlusPlus

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments

MP4 To GIF
: help you to convert mp4 to gif file.

X
A prohibited operation
0
Would love your thoughts, please comment.x
()
x