加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-16 10:41:36 所属栏目:百科 来源:网络整理
导读:我试图使用以下代码初始化c 11中的字符串列表,并且由于各种原因而失败.错误说我需要使用构造函数来初始化列表,我应该使用list string之类的东西吗? s =新列表 string [尺寸]?我在这里错过了什么? #includestring#includelist#includeiostreamusing namesp
我试图使用以下代码初始化c 11中的字符串列表,并且由于各种原因而失败.错误说我需要使用构造函数来初始化列表,我应该使用list< string>之类的东西吗? s =新列表< string> [尺寸]?我在这里错过了什么?

#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;
 }

I / O是

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"};

解决方法

如果使用gcc,则使用c 98而不是c 11的编译器

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

你可以用gnu 11代替c 11

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读