在 c 11 中初始化字符串列表

新手上路,请多包涵

我试图使用以下代码在 c++11 中初始化字符串列表,但由于各种原因而失败。错误说我需要使用构造函数来初始化列表,我应该使用 list<string> s = new list<string> [size] 类的东西吗?我在这里想念什么?

 #include<string>
#include<list>
#include<iostream>
using namespace std;

int main() {
      string s = "Mark";
      list<string> l  {"name of the guy"," is Mark"};
      cout<<s<<endl;
      int size = sizeof(l)/sizeof(l[0]);
      for (int i=0;i<size;i++) {
             cout<<l[i]<<endl;
      }
      return 0;
 }

输入/输出是

 strtest.cpp:8:47: error: in C++98 ‘l’ must be initialized by constructor, not
 by ‘{...}’
 list<string> l  {"name of the guy"," is Mark"};

原文由 Aparna Chaganti 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 577
1 个回答

您使用的是 c++98 而不是 c++11 的编译器。如果您使用的是 gcc,请使用它

g++ -std=c++11 -o strtest strtest.cpp

你可以用 gnu++11 替换 c++ 11

原文由 K.Juce 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
logo
Stack Overflow 翻译
子站问答
访问
宣传栏