C++ 对列表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24811418/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C++ List of Pairs
提问by Vlad from Moscow
While getting comfortable with the C++ STL I ran into this issue using a list of pair objects.
在熟悉 C++ STL 的同时,我使用配对对象列表遇到了这个问题。
int count (std::string input, std::vector<std::string> screen){
std::string::iterator it = input.begin();
std::pair<std::string, int> freq[10];
std::list<std::pair<std::string,int> > freq2;
freq2.push_back(std::make_pair(*it,0)); //ERR:No instance of fn matches arg list
freq2.push_back(std::make_pair('S',0)); //ERR:No instance of fn matches arg list
freq2.push_back(std::make_pair("S",0)); //This is fine
freq[0] = std::make_pair(*it,0); //This is fine
freq[0] = std::make_pair("S",0); //This is fine
freq[0] = std::make_pair('S',0); //This is fine
return 1;
}
Both freq and freq2 are quite similar except freq2 is just a list. Freq is able to accept chars, strings, and the iterator pointer and both pairs (freq,freq2) are declared as pairs. Not sure why this is happening, any tips? Thanks.
freq 和 freq2 非常相似,除了 freq2 只是一个列表。Freq 能够接受字符、字符串和迭代器指针,并且两个对 (freq,freq2) 都被声明为对。不知道为什么会这样,有什么提示吗?谢谢。
edit: It makes a lot more sense now, all the responses I got were really helpful, thank you guys!
编辑:现在更有意义了,我得到的所有回复都非常有帮助,谢谢你们!
回答by Slava
There is no implicit constructor in std::string
that accepts char
and can be used to convert type char
to std::string
. What you can use is this constructor:
没有隐式构造函数std::string
接受char
并且可用于将类型转换char
为std::string
. 你可以使用的是这个构造函数:
basic_string( size_type count, CharT ch, const Allocator& alloc = Allocator() );
basic_string( size_type count, CharT ch, const Allocator& alloc = Allocator() );
so
所以
freq2.push_back(std::make_pair( std::string( 1, *it ),0));
and so on
等等
回答by Vlad from Moscow
Class std::basic_string
has no constructor that has one parameter of type char
.So these statemenets are invalid
类std::basic_string
没有具有一个类型参数的构造函数char
。所以这些语句是无效的
freq2.push_back(std::make_pair(*it,0)); //ERR:No instance of fn matches arg list
freq2.push_back(std::make_pair('S',0)); //ERR:No instance of fn matches arg list
Speaking more precisely in these statements there is an attempt to build an object of type std::pair<std::string, int>
from an object of type std::pair<char, int>
. This requires that there would be a constructor of class std::string
that has parameter of type char
. However there is no such a constructor.
更准确地说,在这些语句中,尝试std::pair<std::string, int>
从type的对象构建 type的对象std::pair<char, int>
。这要求将有一个std::string
具有类型参数的类的构造函数char
。但是没有这样的构造函数。
As for these statements
至于这些说法
freq[0] = std::make_pair(*it,0); //This is fine
freq[0] = std::make_pair('S',0); //This is fine
then there is used an assignment operator of class std::basic_string
. The class has an overloaded assignment operator that accepts an object of type char
.
然后使用了 class 的赋值运算符std::basic_string
。该类有一个重载的赋值运算符,它接受类型为 的对象char
。
basic_string& operator=(charT c);
So these statements are correct.
所以这些说法是正确的。
Consider the following example
考虑下面的例子
#include <iostream>
#include <string>
#include <utility>
int main()
{
std::pair<std::string, int> p1 = { "a", 1 };
std::pair<char, int> p2 = { 'B', 2 };
p1 = p2;
std::cout << p1.first << '\t' << p1.second << std::endl;
return 0;
}
The output is
输出是
B 2
As class std::pair
has a template assignment operator and class std::string
in turn has assignment operator that has parameter of type char then this code is compiled successfully.
由于类std::pair
具有模板赋值运算符,而类std::string
又具有具有 char 类型参数的赋值运算符,因此此代码编译成功。
If you would write for example
如果你会写例如
std::pair<std::string, int> p1 = std::make_pair( 'a', 1 );
instead of
代替
std::pair<std::string, int> p1 = { "a", 1 };
you would get an error because there is no conversion from std::pair<char, int>
to std::pair<std::string, int>
because there is no constructor from char
to std::string
.
你会得到一个错误,因为没有从std::pair<char, int>
to 的转换,std::pair<std::string, int>
因为没有构造函数 from char
to std::string
。
回答by Ian T. Small
You seem to be mixing char and std::string types. String iterators point to individual char values, not a std::string. Review http://www.cplusplus.com/reference/string/string/begin/for details.
您似乎混合了 char 和 std::string 类型。字符串迭代器指向单个字符值,而不是 std::string。查看http://www.cplusplus.com/reference/string/string/begin/了解详细信息。
Also, 'S'
is NOT equivilent to "S"
as the first is of type char while the second is a of type char* which points to an address in memory which contains a 'S'
followed by a '\0'
(the "null character" which is NOT the same as null).
此外,'S'
不等于"S"
第一个是 char 类型,而第二个是 char* 类型,它指向内存中的地址,其中包含'S'
后跟 a '\0'
(与 null 不同的“空字符”)。
freq2.push_back(std::make_pair(*it,0)); //The iterator is pointing to a literal character value, and not a string
freq2.push_back(std::make_pair('S',0)); //This is a literal character, and not a string literal
freq2.push_back(std::make_pair("S",0)); //This is fine and what you intended
The assignments to the array do so via an implicit conversion. See http://www.cplusplus.com/reference/utility/make_pair/.
对数组的赋值是通过隐式转换完成的。请参阅http://www.cplusplus.com/reference/utility/make_pair/。
freq[0] = std::make_pair(*it,0); //Implicit conversion from Pair<char, int>
freq[0] = std::make_pair("S",0); //Implicit conversion from Pair<char*, int>
freq[0] = std::make_pair('S',0); //Implicit conversion from Pair<char, int>
A potentialwork around if you actually needthis conversion functionality would be to create a variable of type std::pair<std::string, int>
and then pushing the value onto the list. I would only recommend this in a desperate and odd case though, and instead correct the intent of the code directly.
如果您确实需要此转换功能,一个潜在的解决方法是创建一个类型的变量,然后将值推送到列表中。我只会在绝望和奇怪的情况下推荐这个,而是直接更正代码的意图。std::pair<std::string, int>
回答by PaulMcKenzie
The issue is that you're attempting to construct a std::string
from a single char. There is no such constructor for std::string
.
问题是您试图std::string
从单个字符构造 a 。没有这样的构造函数std::string
。