简体   繁体   English

从带有 std::tuple 的 map 中输出

[英]Cout from a map with std::tuple

I have made a small map that I call BMW .我做了一个小的 map,我称之为BMW It contains the keys Usage and Diesel , as shown below.它包含键UsageDiesel ,如下所示。

#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).我要做的是在 map 中找到关键的Usage ,然后打印出包含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.我尝试使用的代码不起作用,我无法在 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) (字符串稍后将在适当的时候转换为 int,但由于技术原因,我现在想将它们作为字符串读取)

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

These are single quotes .这些是单引号

Single quotes delimit char literals.单引号分隔char文字。

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.错误消息说您正在尝试使用不存在的find(int)重载的原因是,实际上包含多个字符的char文字是一种特殊的东西,称为“多字符文字”, int类型和实现定义的值。 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.接下来,您将遇到元组没有内置cout格式的问题。 You will have to take BMW.find("Usage")->second and give it to some function that prints the contents in whatever way you want.您将不得不接受BMW.find("Usage")->second并将其提供给 function 以您想要的任何方式打印内容。

For starters if you are using the member function find then you should check whether the data is indeed found.对于初学者,如果您使用成员 function find ,那么您应该检查是否确实找到了数据。

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 .对于std::tuple类型的 object 没有运算符 <<。 You have to output individual data members of the tuple.您必须 output 元组的单个数据成员。 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它的 output 是

Usage => ( 1, 2 )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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