我只是对map :: count()和map :: erase()的使用有一个简单的问题.我使用这些与std :: string,但想知道我是否需要使用string :: c_str().例如,我的代码当前显示为:
void Person::removeFriend(std::string first, std::string last){
std::string name = (first + last);
//checks to ensure the friend exists in the user's friend list
if (_friends.count(name) == 1){
_friends.erase(name);
}
}
Run Code Online (Sandbox Code Playgroud)
所以我的问题是,如果它实际上显示为:
void Person::removeFriend(std::string first, std::string last){
std::string name = (first + last);
//checks to ensure the friend exists in the user's friend list
if (_friends.count(name.c_str()) == 1){
_friends.erase(name.c_str());
}
}
Run Code Online (Sandbox Code Playgroud)
另外,我想这也适用于map :: insert().我只知道使用std :: string打开文件的这种用法.任何和所有的建议都提前非常感谢!
我有一个头文件,其中声明了一个void函数.在源文件中,我已经为此函数编写了实现.在编译项目时,我收到一个错误,指示我的实现与标头中的原型不匹配.
头文件(Dictionary.h)中的代码显示为:
void spellCheck(ifstream checkFile, string fileName, std::ostream &out);
Run Code Online (Sandbox Code Playgroud)
源文件(Dictionary.cpp)中的代码显示为:
Dictionary::spellCheck(ifstream checkFile, string fileName, std::ostream &out){
_report.setFileName(fileName);
string end = "\n";
int words = 0;
int wrong = 0;
string word;
char currChar;
while(checkFile >> word){
currChar = checkFile.get();
while(currChar != "\\s" && currChar == "\\w"){
word += currChar;
currChar = checkFile.get();
}
out << word << end;
word.clear();
}
/*
_report.setWordsRead(words);
_report.setWordsWrong(wrong);
_report.printReport();
*/
}
Run Code Online (Sandbox Code Playgroud)
这里有什么可能表明我试图返回一个整数值吗?
确切的错误是:
Dictionary.cpp:31:1: error: prototype for 'int Dictionary::spellCheck(std::ifstream, std::string, std::ostream&)' does not match any …Run Code Online (Sandbox Code Playgroud)