C++ STL List Iterator

Muh*_*han 1 c++ iterator stl compiler-errors list

我正在尝试使用以下代码遍历字符串列表:

#include<cstdlib>
#include<string>
#include<list>

using namespace std;

list<string> dict = {"aardvark", "ambulance", "canticle", "consumerism"};
list<string> bWords = {"bathos", "balderdash"};
//splice the bWords list into the appropriate spot in dict
auto iterLastB = --(bWords.end());
//find spot in dict
list<string>::iterator it = dict.begin();
while(it != dict.end()){
  if(*it > *iterLastB)
    break;
    ++it;
}
dict.splice(it, bWords);
Run Code Online (Sandbox Code Playgroud)

然而,在构建这个时,我得到错误expected unqualified-id before 'while' 这是什么意思,我该如何解决这个问题?

cni*_*tar 8

你不能直接编写代码.至少你需要一个main功能.您应该在main函数中添加所有内容(包括除外).

  • 我觉得奇怪的是OP如何编写如上所述的代码而忘记了`main`. (4认同)