use*_*898 0 c++ stdstring stdvector
我有配置,我需要设置到某种容器我尝试设置为std :: vector但我在两种方式获得编译错误:
std::vector<std::string> ConfigVec= new std::vector<std::string>();
ConfigVec->at(0).append("00000\
11111\
00000");
ConfigVec->at(1) = "11111\
00000\
00000";
Run Code Online (Sandbox Code Playgroud)
没有很多std :: string声明,最简单的方法是什么
首先,删除指针和new1.其次,您要附加不存在的元素.将字符串推回向量.
std::vector<std::string> ConfigVec;
ConfigVec.push_back("000001111100000");
ConfigVec.push_back("111110000000000");
Run Code Online (Sandbox Code Playgroud)
等等.
如果你有少量的字符串,你可以直接初始化向量(除非你坚持使用pre-C++ 11实现):
std::vector<std::string> ConfigVec{"000001111100000", "111110000000000"};
Run Code Online (Sandbox Code Playgroud)
1*您使用的ConfigVec是指针(将结果分配new给它,并使用它->来访问其成员),但它实际上并未声明为一个.这本身就是一个错误.在任何情况下,使用new和原始指针在C++中动态分配资源的情况都很少.