动态字符串数组c++
dynamic string array c++
我创建了一个固定长度的字符串:
string fileRows[900];
但有时我需要超过900,有时500就足够了。
之后,我需要用文件行填充数组:
...
string sIn;
int i = 1;
ifstream infile;
infile.open(szFileName);
infile.seekg(0,ios::beg);
while ( getline(infile,sIn ) ) // 0. elembe kiterjesztés
{
fileRows[i] = sIn;
i++;
}
如何为这个数组创建动态长度?
使用std::vector, vector被称为动态数组:
#include <vector>
#include <string>
std::vector<std::string> fileRows(900);
实际上你可以为元素保留空间并调用push_back
:
std::vector<std::string> fileRows;
fileRows.reserve(900);
while (std::getline(infile, sIn))
{
fileRows.push_back(sIn);
}
相关文章:
- Mongodb c++驱动程序:如何查询元素的数组
- 将数组的地址分配给变量并删除
- 从C++本机插件更新Vector3数组
- lambda参数转换为constexpr技巧,然后获取带链接的数组
- 将数组作为参数传递给函数安全吗?作为第三方职能部门,可以探索他们想要的之外的其他元素
- 数组索引的值没有增加
- 将对象数组的引用传递给函数
- 为char数组调整zlib-zpipe
- 2D数组来自文本输入,中间有空格
- std::向量与传递值的动态数组
- 在c++中用vector填充一个简单的动态数组
- 使用strcpy将char数组的元素复制到另一个数组
- 使用指针从C++中的数组中获取最大值
- C++使用整数的压缩数组初始化对象
- 告诉一个 const char 数组,除了编译时 C 样式的字符串外,它不以 '