问题:出现在哪个地方?关于map的搜索问题
生活随笔
收集整理的這篇文章主要介紹了
问题:出现在哪个地方?关于map的搜索问题
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
#include <iostream>
#include <string>
#include <map>
#include <set>
using namespace std;
const string &trans(map<string,string> &m,const string &s){//問題在下面的(auto pos = m.find(s) != m.end())把pos定義放在外面就可以了if((auto pos = m.find(s)) != m.end()){return s;}elsereturn "good"; }
int main()
{
map<string,string> m;return 0;
}
//g++編譯結果
r@r:~/coml/c++/11/11.3/11.3.6/myself_word_transform$ g++ test.cc -o 123
test.cc: In function ‘const string& trans(std::map<std::__cxx11::basic_string<char>, std::__cxx11::basic_string<char> >&, const string&)’:
test.cc:8:10: error: expected primary-expression before ‘auto’8 | if((auto pos = m.find(s)) != m.end())| ^~~~
test.cc:8:10: error: expected ‘)’ before ‘auto’8 | if((auto pos = m.find(s)) != m.end())| ~^~~~| )
test.cc:12:3: error: expected ‘)’ before ‘else’12 | }| ^| )13 | else| ~~~~
test.cc:8:8: note: to match this ‘(’8 | if((auto pos = m.find(s)) != m.end())| ^
test.cc:17:3: warning: no return statement in function returning non-void [-Wreturn-type]17 | }| ^
問題已經解決;代碼中顯示的,把pos定義放在外面就可以了
總結:if語句中,括號內可以有賦值語句,但是不能有定義語句。
總結
以上是生活随笔為你收集整理的问题:出现在哪个地方?关于map的搜索问题的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 关于map的下标操作的2个例子
- 下一篇: c++ primer 5th,习题11.