Searching and Inserting in a map with 3 elements in C++

后端 未结 3 1359
情话喂你
情话喂你 2021-01-02 20:47

I need to have a map like this :

typedef std::map Maptype ;

What is the syntax to insert and searching elements of

3条回答
  •  刺人心
    刺人心 (楼主)
    2021-01-02 21:22

    You cannot have three elements. The STL map stores a key-value pair. You need to decide on what you are going to use as a key. Once done, you can probably nest the other two in a separate map and use it as:

    typedef std::map > MapType;
    

    In order to insert in a map, use the operator[] or the insert member function. You can search for using the find member function.

    MapType m;
    // insert
    m.insert(std::make_pair(4, std::make_pair(3.2, 'a')));
    m[ -4 ] = make_pair(2.4, 'z');
    // fnd
    MapType::iterator i = m.find(-4);
    if (i != m.end()) { // item exists ...
    }
    

    Additionally you can look at Boost.Tuple.

提交回复
热议问题