错误 C2679:二进制'=':找不到采用类型为"std::basic_string的右侧操作数的运算符
error C2679: binary '=' : no operator found which takes a right-hand operand of type 'std::basic_string
我曾尝试使用unordered_map
的C++代码编写电话簿,但遇到了问题。在这段代码的开头有一个我无法理解的错误。当我试图运行它时,我得到了这个错误:
错误1错误C2679:二进制"="
有人能帮我吗?
#include <iostream>
#include <string>
#include <unordered_map>
#include <vector>
#include <hash_map>
#include <algorithm>
using namespace std;
int main()
{
unordered_map<string, pair< string,vector<string>>>contact;
string name, number, address;
cin >> name >> number>>address;
contact[name]=make_pair(number, address);
unordered_map<string, pair< string, vector<string>>> ::iterator it;
it = contact.begin();
while (it != contact.end())
{
cout << it->first;
it++;
}
return 0;
}
您将contact
声明为unordered_map<string, pair< string,vector<string>>>
此处插入contact[name]=make_pair(number, address);
contact[string] = make_pair(string, string)
。必须是contact[string] = make_pair(string, vector<string>)
要么改变容器的定义,比如
unordered_map<string, pair< string, string>>contact;
或者,如果你真的需要你的address
成为vector
,那么
contact[name] = make_pair(number, std::vector<string>{address});
如果您使用c++11
或更高版本,则可以使用auto
,
auto it = contact.begin();
而不是
unordered_map<string, pair< string, vector<string>>> ::iterator it;
it = contact.begin();
相关文章:
- cppcheck在const std::string[]上引发警告
- 将std::string传递给WriteConsole API
- 为std::string的某个索引赋值
- std中有类似find_last_of的函数,而string中没有
- 使用 std::string () const 函数启动线程或未来
- 使用char类型将decimal转换为string,将string转换为decimal
- 如何在Visual Basic中使用矩形函数OpenGL绘制矩形
- 迭代和比较映射<字符串、矢量<string>> c++ 中的值
- 当我们进行一些操作时,应该使用什么'std::string'或'std::stringstream'?
- 将向量解析<string>为字符串
- 'string.assign(string.data(), 5)' 是明确定义的还是 UB?
- 如何更改大小(std::string)
- "string.h"在构建适用于iOS的qt应用程序中找不到消息
- C++:如何将 unix 时间的字符串转换为 *tm?(使用时间错误:"cannot convert 'String' to 'tm*' ")
- std::string 的对象真的可以移动吗?
- 与'operator='不匹配(操作数类型'String'且"void")
- SegFault 同时使用 std::string::operator+= 和函数作为参数
- 无法从 std::string 中提取C++ Unicode 符号
- std::string 构造函数如何处理固定大小的 char[]?
- <string> 使用 for 循环写入向量