将字符串标记存储到数组中
Storing a string token into an array
我使用C++使用分隔符标记字符串,并且我可以在while循环中使用cout输出当前标记。我想做的是将令牌的当前值存储在一个数组中,以便以后可以访问它。这是我现在的代码:
string s = "Test>=Test>=Test";
string delimiter = ">=";
vector<string> Log;
int Count = 0;
size_t pos = 0;
string token;
while ((pos = s.find(delimiter)) != string::npos) {
token = s.substr(0, pos);
strcpy(Log[Count].c_str(), token.c_str());
Count++;
s.erase(0, pos + delimiter.length());
}
只需在向量上使用push_back
。它将为您将令牌复制到向量中。无需计数;无需strcpy
:
string s = "Test>=Test>=Test";
string delimiter = ">=";
vector<string> Log;
size_t pos = 0;
string token;
while ((pos = s.find(delimiter)) != string::npos) {
token = s.substr(0, pos);
Log.push_back(token);
s.erase(0, pos + delimiter.length());
}
push_back()
将满足您的需要。
相关文章:
- 多维数组存储三种不同的数据类型?
- C++将原始字符数组存储在元组中
- 将十六进制字符数组存储在字节数组中,而无需更改为 ASCII 或其他任何东西
- 我正在编写一个代码来将 int 数组存储在文件中,然后用 c++ 检索它,但是检索第一项是假值,我该如何解决这个问题?
- 如何将元素的索引从2D数组存储到1D数组中,然后交换这些值
- 如何将JSON数组存储在Arduino程序中
- 如何在AVR(例如:ATMega)或Arduino微控制器上制作2D PROGMEM阵列的1D PROGMEM数组(存储
- 使用 char 数组存储数据
- 使用平面数组存储的元组容器
- 数组 存储并要求用户搜索它
- 将值从一个 char 数组存储到另一个 char 数组
- 如何使字符串数组存储在向量中
- 将构造的静态数组存储在多个文件中的内存中
- 将整数数组存储在堆上并通过指针访问它们
- c++声明一个数据类型的字节数组,该数组存储在字符串中
- 使用指针数组存储阶乘计算的所有中间结果
- 使用异构数组存储不同的子类
- 读取文件内容,存储在数组中,向数组添加更多内容,然后将新数组存储在文件C++中
- 尝试将复杂数组存储到类 c++ 中
- 在字节数组存储中嵌入 int/string