|
这个是基础教程的52课的练习题,准备把 str1传给str2,结果发现不行。顺便大佬们看看我这段是不是符合练习题的解答
int main()
{
// 将以下字符串以#号为分隔符进行分割,将分割后的各个子串的结果存储到一个 vector 中,字符串如下:
// 123#ab##cctry.com#
string str1 = "123#ab#cctry.com#";
vector<string> str2;
int i = 1;
while (i >= 0)
{
i = str1.find('#');
if (i >= 0) str1.erase(i, 1);
}
cout << str1 << endl;
system("pause");
return 0;
};
做了一宿,终于搞出来了
- #include <vector>
- #include <string>
- #include <iostream>
- using namespace std;
- int main(int argc, char* argv[])
- {
- string str1 = "#123#ab#cctry.com#2";
- vector<string> arr_str;
- int i = 0;
- while (i >= 0)
- {
- i = str1.find('#');
- if (i < 0) break;
- string sub_str = str1.substr(0, i);
- if (sub_str.size() > 0) arr_str.push_back(sub_str);
- str1.erase(0, i + 1);
- }
- if (str1.size() > 0) arr_str.push_back(str1);
- //打印输出
- for (size_t idx = 0; idx < arr_str.size(); ++idx)
- {
- cout << arr_str[idx] << endl;
- }
- system("pause");
- return 0;
- }
复制代码
|
上一篇: 求类推荐下一篇: syc站长界面开发第二步的问题
|