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

在c 11中初始化字符串列表,第1张

概述我试图使用以下代码初始化c 11中的字符串列表,并且由于各种原因而失败.错误说我需要使用构造函数来初始化列表,我应该使用list< string>之类的东西吗? s =新列表< string> [尺寸]?我在这里错过了什么? #include<string>#include<list>#include<iostream>using namespace std;int main() { 我试图使用以下代码初始化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

总结

以上是内存溢出为你收集整理的在c 11中初始化字符串列表全部内容,希望文章能够帮你解决在c 11中初始化字符串列表所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1222106.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-05
下一篇 2022-06-05

发表评论

登录后才能评论

评论列表(0条)

保存