如何设置std::vector<std::string>每个元素中的自定义字符串
How to set std::vector<std::string> with custom string in each element
>我需要将配置设置到某种容器中我尝试设置标准::矢量但是我以两种方式收到编译错误:
std::vector<std::string> ConfigVec= new std::vector<std::string>();
ConfigVec->at(0).append("00000
11111
00000");
ConfigVec->at(1) = "11111
00000
00000";
没有很多 std::string 声明的最短方法是什么
首先,删除指针并new
1。其次,你正在追加到不存在的元素。将字符串推回矢量。
std::vector<std::string> ConfigVec;
ConfigVec.push_back("000001111100000");
ConfigVec.push_back("111110000000000");
等等。
如果你有少量字符串,你可以直接初始化向量(除非你坚持使用 C++11 之前的实现):
std::vector<std::string> ConfigVec{"000001111100000", "111110000000000"};
1 * 您使用 ConfigVec
作为指针(将new
的结果分配给它,并使用 ->
访问其成员),但它实际上并未声明为指针。这本身就是一个错误。无论如何,很少有理由使用 new
和原始指针来动态分配C++资源。
std::vector<std::string> ConfigVec= new std::vector<std::string>();
这是"java-nese":std::vector<std::string> ConfigVec
只是字符串本身的vectcor。 ConfigVect.push_back("text")
只会在末尾添加一个字符串。
还是你的意思
std::vector<std::string>* ConfigVec= new std::vector<std::string>();
//----------------------^-----------<< note taht!
在任何情况下,您都不能在空向量上使用at
(或[]
),请首先适当调整其大小
相关文章:
- 使用std::transform将一个范围的元素添加到另一个范围中
- 有没有办法将谓词中的元素偏移量传递给 std 算法?
- 我想访问std::unique_ptr中的一个特定元素
- 如何从存储在std::映射中的std::集中删除元素
- 缓存std::数组的选定元素,并在c++中自动保持其一致性
- 从嵌套在std::映射中的std::列表中删除元素的最佳方式
- 使用运算符 [] 引用 std::vector 上最后一个元素时出现问题<>
- 对的排序向量 (std::vector<pair<int, int>>) 按对的第一个元素搜索并更新第二个元素值
- 为什么在 std::map 上移动无法将元素从一个映射移动到另一个映射
- 如何为 std::vector 分配内存,然后稍后为某些元素调用构造函数?
- std::矢量保存 只推送最后一个元素
- std::map:当元素不可默认构造时创建/替换元素
- 如何读/写或遍历 std::array 中的特定元素范围?
- C++如何乘以包含 std::variant 元素的向量的迭代器?正在执行迭代器类型的转换?
- 为什么 std::p air 的大小与其元素的大小之和不同?
- 我可以列表初始化 std::vector 并完美转发元素吗?
- 使用 std::min_element、std::max_element 查找矢量中的最小和最大元素
- 是否有一个 std::set 函数来确定不超过数字 x 的最大元素?
- 除了 std::vector 之外,是否有一个 std 容器不会复制和销毁作为类的元素?
- call call cons构件功能的std ::元素的每个元素