C语言c2679错误,STL在C ++中映射错误C2679(STL Map error C2679 in C++)

本文探讨了在C++中使用STL映射时遇到的编译错误C2679,详细分析了如何正确输出int与string对,并提供了修正代码及必要的头文件包含。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

STL在C ++中映射错误C2679(STL Map error C2679 in C++)

即时通讯在尝试在STL Map中输出int&string对时遇到问题:

这是使用以下代码:

#include

#include

#include

using namespace std;

typedef map intID;

int main(){

intID ID;

ID.insert(pair(123,"studentname1"));

ID.insert(pair(124,"studentname2"));

ID.insert(pair(122,"studentname3"));

intID::iterator IDIter;

for(IDIter = ID.begin();IDIter != ID.end();++IDIter){

cout <first <second << endl;

}

}

错误发生在", Name: " << IDIter->second部分, <

编译错误是:

错误1错误C2679:二进制'<

我试图打印出对的第二个成员(studentname)我是新来的STL映射,所以我不知道我做错了什么,我需要改变什么?

im having a problem when trying to print out pairs of int & strings in an STL Map:

this is the code im using:

#include

#include

#include

using namespace std;

typedef map intID;

int main(){

intID ID;

ID.insert(pair(123,"studentname1"));

ID.insert(pair(124,"studentname2"));

ID.insert(pair(122,"studentname3"));

intID::iterator IDIter;

for(IDIter = ID.begin();IDIter != ID.end();++IDIter){

cout <first <second << endl;

}

}

The error occurs at the ", Name: " << IDIter->second part, the << is underlined saying "no operator matches these operands"

the compile error is:

Error 1 error C2679: binary '<

Im trying to print out the second member of the pair (studentname) I'm new to STL mapping so i'm not sure what i'm doing wrong, what do i need to change?

原文:https://stackoverflow.com/questions/16399813

更新时间:2019-12-31 07:15

最满意答案

您需要包含标题。 你只能通过偶尔包含其他头文件来使用std::string类型。 你不能依赖这个。 包含也会引入operator<

You need to include the header. You've only been able to use the std::string type by incidental inclusion from other headers. You can't rely on this. Including will also bring in the overloads of operator<< that allow you to output strings.

2013-05-06

相关问答

一般来说,你不应该混合这两种编码。 但是,异常消息只是开发人员感兴趣的东西(例如,在日志文件中),不应该向用户显示(但请注意Jim的重要注意事项)。 因此,如果您使用UNICODE作为整个面向用户的界面,并仍在开发人员消息的幕后使用std::exception等,那么您就安全起来了。 应该不需要在两者之间进行转换。 此外,在C ++中为UNICODE独立的字符串定义typedef是一个很好的技巧: typedef std::basic_string tstring;

...并类似地

...

名称left和right函数已存在于ios标头中。 他们是IO操纵者。 因此,这就是为什么你应该使用显式命名空间解析而不是仅using namespace std编写。 当然,您应该避免使用全局变量。 Functions with name left and right already exists in ios header. They are IO manipulators. So, this is why you should use explicit namespace resolutio

...

您需要包含标题。 你只能通过偶尔包含其他头文件来使用std::string类型。 你不能依赖这个。 包含也会引入operator< header. You've only been able to use the std::string type by incidental inclusion from other headers. You can't rely on th

...

这是正确的编译版本: class Line

{

std::string data;

public:

friend std::istream& operator>>(std::istream& inputStream, Line& line)

{

std::getline(inputStream, line.data);

return inputStream;

}

op

...

您的问题可以归结为: struct Foo {};

Foo operator+(Foo&, int&) { return Foo(); }

int main()

{

Foo f1;

Foo f2 = f1 + 42;

}

您的参数是非const引用,并且您传递的参数不能绑定到这些参数(在我的示例中为42,在您的示例中为5)。 您可以修改此操作符将参数更改为const引用。 这无论如何都是有道理的,因为二元运算符不应该真正改变它的操作数。 上面的例子可以修改如下: Foo operat

...

获取指向托管类型的指针的shared_ptr构造函数是explicit template explicit shared_ptr(Y * p);

这可以防止从KingChatFilter *返回的KingChatFilter *到下面的赋值中的shared_ptr的隐式转换: filter = new KingChatFilter;

首选解决方案是用。替换赋值 filter = boost::make_shared

...

m1类型为Matrix ,因此在查找适当的operator/重载时,我们会发现: Matrix Matrix::operator/(const Matrix& denom);

请注意,此处的参数类型Matrix使用所谓的注入类名。 这意味着Matrix在这种情况下代表Matrix ,因为那是所讨论的(模板)类。 但是m2 ,调用operator/的参数的类型为Matrix 。 从Matrix到Matrix没有合适的

...

begin将返回一个迭代器,您尝试将其分配给char 。 要么取消引用迭代器 szName = *(buffer.begin());

或者只是打电话给front szName = buffer.front();

编辑 如果szName 不是 char而是std::string ,那么实际应该执行以下操作来构造std::string szName = std::string(buffer.begin(), buffer.end());

begin will return an iterat

...

由于Cell有一个无参数构造函数的声明 Cell cells[80][72];

定义一个80x72的Cell对象数组。 已经为您构造了对象,因此嵌套的for循环是不必要的。 另一方面,如果您将Cell声明为指向Cell的80x72指针数组,即 Cell* cells[80][72];

然后你必须像你想要的那样分配每一个。 如果你实际上不需要使用指针,那就不要。 Since Cell has a parameterless constructor the statement Cell cells

...

不要使用避免使用std::copy和std::map ,这太复杂了,不能使它工作,它会使旧的VS编译器产生错误信息。 替代方案: y.insert(x.begin(), x.end());

如果失败,您应该收到更明确的错误消息。 Don't use Avoid using std::copy with std::map, way too complex to make it work it make error messages cryptic with old VS compilers. Al

...

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值