C++ 映射及其内容的问题
Trouble with C++ maps and their content
这是相当广泛的,所以提前,如果你通过这个,即使没有答案或解决方案,谢谢。
所以,我有这个程序,它是一个基本的社交网络,减去用户界面,其中用户由 Person
对象表示,该对象负责维护朋友列表、一个阻止列表、一个消息列表和一个待处理的好友请求队列。这些列表分别属于 std::map<std::string, Person>
、std::map<std::string, Person>
、std::vector<Message>
和 std::queue<Message>
类型,其中两个地图的 std::string
是用户的最后两个容器中的名字和姓氏以及 Message
是我定义的附加 class。我为 Person
重载了 <<
运算符,以便它打印用户的名字和姓氏,中间有一个 space。出于某种原因,当我打印出这些名字时,return 是空的,我不知道为什么。下面的代码本质上是对正在发生的事情的演练。
我用来测试主代码的行 class:
std::string cFirst ("Chris");
std::string cLast ("Cringle");
SocialNetwork sn;
sn.addUser(cFirst,cLast);
SocialNetwork
中的addUser()
函数:
void SocialNetwork::addUser(std::string first, std::string last){
std::string name = (first + last);
Person user (first, last);
_users.insert(std::pair<std::string, Person>(name, user));
}
其中 _users
是 SocialNetwork
类型 std::map<std::string, Person>
上的成员数据。 Person
的构造函数是:
Person::Person(std::string first, std::string last){
_first = first;
_last = last;
}
其中 _first
和 _last
是 Person
上的成员数据,代表用户的名字和姓氏。然后,回到主 class,在 sn.addUser(cFirst,cLast);
:
之后
sn.printUsers();
看起来像:
void SocialNetwork::printUsers(){
std::map<std::string, Person>::iterator it;
it = _users.begin();
while(it != _users.end()){
cout << it->first << endl;
cout << it->second << endl;
it++;
}
}
根据我的给定代码,cout << it->first << endl;
的预期输出应该是 ChrisCringle
,确实如此。 cout << it->second << endl;
的预期输出应该调用重载运算符并且应该是 Chris Cringle
,但它只是打印一个空白 space。任何关于原因的迹象将不胜感激。我需要通过引用传递我的参数吗?我已经试过了,似乎 运行 遇到了很多麻烦。如果似乎缺少某些可能有帮助的东西,请随时提出!再次感谢!我知道我可能会因为这个长问题而受到很多批评,但我不认为我可以设法使这个问题变得更简单。
编辑:重载运算符的代码是:
ostream& operator<<(ostream& os, const Person& per){
os << per._first << " " << per._last;
return os;
}
我刚刚使用了您显示的所有代码:http://ideone.com/mFBxTC
#include <string>
#include <iostream>
#include <map>
using namespace std;
struct Person {
Person(std::string first, std::string last);
std::string _first, _last;
};
ostream& operator<<(ostream& os, const Person& per){
os << per._first << " " << per._last;
return os;
}
struct SocialNetwork {
void addUser(std::string first, std::string last);
std::map<std::string, Person> _users;
void printUsers();
};
void SocialNetwork::addUser(std::string first, std::string last){
std::string name = (first + last);
Person user (first, last);
_users.insert(std::pair<std::string, Person>(name, user));
}
Person::Person(std::string first, std::string last){
_first = first;
_last = last;
}
void SocialNetwork::printUsers(){
std::map<std::string, Person>::iterator it;
it = _users.begin();
while(it != _users.end()){
cout << it->first << endl;
cout << it->second << endl;
it++;
}
}
int main() {
std::string cFirst ("Chris");
std::string cLast ("Cringle");
SocialNetwork sn;
sn.addUser(cFirst,cLast);
sn.printUsers();
return 0;
}
而且效果很好。所以错误在别处
这就是为什么在 post 调试问题时应该 post SSCCE。
这是相当广泛的,所以提前,如果你通过这个,即使没有答案或解决方案,谢谢。
所以,我有这个程序,它是一个基本的社交网络,减去用户界面,其中用户由 Person
对象表示,该对象负责维护朋友列表、一个阻止列表、一个消息列表和一个待处理的好友请求队列。这些列表分别属于 std::map<std::string, Person>
、std::map<std::string, Person>
、std::vector<Message>
和 std::queue<Message>
类型,其中两个地图的 std::string
是用户的最后两个容器中的名字和姓氏以及 Message
是我定义的附加 class。我为 Person
重载了 <<
运算符,以便它打印用户的名字和姓氏,中间有一个 space。出于某种原因,当我打印出这些名字时,return 是空的,我不知道为什么。下面的代码本质上是对正在发生的事情的演练。
我用来测试主代码的行 class:
std::string cFirst ("Chris");
std::string cLast ("Cringle");
SocialNetwork sn;
sn.addUser(cFirst,cLast);
SocialNetwork
中的addUser()
函数:
void SocialNetwork::addUser(std::string first, std::string last){
std::string name = (first + last);
Person user (first, last);
_users.insert(std::pair<std::string, Person>(name, user));
}
其中 _users
是 SocialNetwork
类型 std::map<std::string, Person>
上的成员数据。 Person
的构造函数是:
Person::Person(std::string first, std::string last){
_first = first;
_last = last;
}
其中 _first
和 _last
是 Person
上的成员数据,代表用户的名字和姓氏。然后,回到主 class,在 sn.addUser(cFirst,cLast);
:
sn.printUsers();
看起来像:
void SocialNetwork::printUsers(){
std::map<std::string, Person>::iterator it;
it = _users.begin();
while(it != _users.end()){
cout << it->first << endl;
cout << it->second << endl;
it++;
}
}
根据我的给定代码,cout << it->first << endl;
的预期输出应该是 ChrisCringle
,确实如此。 cout << it->second << endl;
的预期输出应该调用重载运算符并且应该是 Chris Cringle
,但它只是打印一个空白 space。任何关于原因的迹象将不胜感激。我需要通过引用传递我的参数吗?我已经试过了,似乎 运行 遇到了很多麻烦。如果似乎缺少某些可能有帮助的东西,请随时提出!再次感谢!我知道我可能会因为这个长问题而受到很多批评,但我不认为我可以设法使这个问题变得更简单。
编辑:重载运算符的代码是:
ostream& operator<<(ostream& os, const Person& per){
os << per._first << " " << per._last;
return os;
}
我刚刚使用了您显示的所有代码:http://ideone.com/mFBxTC
#include <string>
#include <iostream>
#include <map>
using namespace std;
struct Person {
Person(std::string first, std::string last);
std::string _first, _last;
};
ostream& operator<<(ostream& os, const Person& per){
os << per._first << " " << per._last;
return os;
}
struct SocialNetwork {
void addUser(std::string first, std::string last);
std::map<std::string, Person> _users;
void printUsers();
};
void SocialNetwork::addUser(std::string first, std::string last){
std::string name = (first + last);
Person user (first, last);
_users.insert(std::pair<std::string, Person>(name, user));
}
Person::Person(std::string first, std::string last){
_first = first;
_last = last;
}
void SocialNetwork::printUsers(){
std::map<std::string, Person>::iterator it;
it = _users.begin();
while(it != _users.end()){
cout << it->first << endl;
cout << it->second << endl;
it++;
}
}
int main() {
std::string cFirst ("Chris");
std::string cLast ("Cringle");
SocialNetwork sn;
sn.addUser(cFirst,cLast);
sn.printUsers();
return 0;
}
而且效果很好。所以错误在别处
这就是为什么在 post 调试问题时应该 post SSCCE。