简体   繁体   中英

Cout from a map with std::tuple

I have made a small map that I call BMW . It contains the keys Usage and Diesel , as shown below.

#include <iostream>
#include <bits/stdc++.h>
#include <map>
#include <vector>
using namespace std;

int main()
{

    // initialize container
    std::map<string, std::tuple<string, string>> BMW;

    // insert elements
    BMW.insert({"Usage", {"1", "2"}});
    BMW.insert({"Disel", {"2", "3"}});

    std::cout << "Usage => " << BMW.find('Usage')->second << '\n';

    return 0;
}

What I want to do is to find the key Usage in the map and then print out the strings containing the values for Usage (1, 2). The code I tried with does not work and I am not able to find a good answer why here on Stackoverflow. Here is the error I get:

error: no matching function for call to 'std::map<std::__cxx11::basic_string<char>, std::tuple<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > >::find(int)'|

It would be great if I could be able to get only one of the strings, like the first one, if I want to do that.

(the strings will later be converted to int when this is appropriate, but because of technical reasons I want to read them as strings for now)

BMW.find('Usage')->second
//       ^     ^

These are single quotes .

Single quotes delimit char literals.

You wanted double quotes .

That's because double quotes delimit string literals.


The reason the error message says you're trying to use the non-existent find(int) overload, is that a char literal that actually has more than one character in it is a special thing called a "multi-character literal", of int type and implementation-defined value. You usually don't intend to use these.


Next, you're going to run into the problem that there is no built-in cout formatting for a tuple. You will have to take BMW.find("Usage")->second and give it to some function that prints the contents in whatever way you want.

For starters if you are using the member function find then you should check whether the data is indeed found.

In this expression

BMW.find('Usage')

you are using a multibyte character literal instead of a string literal. The expression must be written like

BMW.find( "Usage" )

There is no operator << for an object of the type std::tuple . You have to output individual data members of the tuple. Here is a demonstrative program.

#include <iostream>
#include <string>
#include <tuple>
#include <map>
#include <iterator>

int main()
{
    std::map<std::string, std::tuple<std::string, std::string>> BMW;

    // insert elements
    BMW.insert( { "Usage", { "1", "2" } } );
    BMW.insert( { "Disel", { "2", "3" } } );

    auto it = BMW.find( "Usage" );

    if ( it != std::end( BMW ) )
    {
        std::cout << "Usage => ( " << std::get<0>( it->second ) << ", " << std::get<1>( it->second ) << " )\n";
    }        
}

Its output is

Usage => ( 1, 2 )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM