#include <vector>
#include <string>
#include <iostream>
using namespace std;
int main()
{
// 初始化变量
string str_cctry = "#123#abc#cctry.com#";
vector<string> vec_cctry;
// 判断字符串中是否存在“#”字符
while (str_cctry.find("#") != -1)
{
// 获取“#”字符存在的位置
int object_num = str_cctry.find("#");
// 根据获取的位置,对字符串进行截取
string sub_cctry = str_cctry.substr(0, object_num);
// 若字符串非空,将截取字符串存储在动态数组中
if (!sub_cctry.empty())
{
vec_cctry.push_back(sub_cctry);
}
// 更新字符串
str_cctry.erase(0, sub_cctry.length() + 1);
}
// 若存在剩余字符串将剩余字符串存储在动态数组中
if (!str_cctry.empty())
{
vec_cctry.push_back(str_cctry);
}
// 循环输出动态数组中存储变量
for (int i = 0; i < int(vec_cctry.size()); i++)
{
cout << vec_cctry[i] << endl;
}
return 0;
}
【千律】C++基础:根据指定字符“#”对字符串进行分割,将分割后的字符串存储在vector动态数组中
于 2022-04-11 20:17:30 首次发布