#include\x20 #include\x20 #include\x20

using\x20namespace\x20std;

int\x20main()\x20{ \x20\x20int\x20n; \x20\x20cin\x20>>\x20n; \x20\x20cin.ignore();\x20//\x20忽略第一行的换行符

\x20\x20vector\x20strings; \x20\x20for\x20(int\x20i\x20=\x200; i\x20<\x20n; i++)\x20{ \x20\x20\x20\x20string\x20s; \x20\x20\x20\x20getline(cin, s);\x20//\x20获取整行输入 \x20\x20\x20\x20strings.push_back(s); \x20\x20}

\x20\x20int\x20count\x20=\x200; \x20\x20for\x20(int\x20i\x20=\x200; i\x20<\x20n; i++)\x20{ \x20\x20\x20\x20string\x20s\x20=\x20strings[i]; \x20\x20\x20\x20for\x20(int\x20j\x20=\x200; j\x20<\x20s.length(); j++)\x20{ \x20\x20\x20\x20\x20\x20if\x20(s[j]\x20==\x20' ')\x20{ \x20\x20\x20\x20\x20\x20\x20\x20count++; \x20\x20\x20\x20\x20\x20\x20\x20s.erase(j, 1);\x20//\x20删除空格 \x20\x20\x20\x20\x20\x20\x20\x20j--;\x20//\x20删除后需要将索引回退一位 \x20\x20\x20\x20\x20\x20} \x20\x20\x20\x20} \x20\x20\x20\x20cout\x20<<\x20s\x20<<\x20endl; \x20\x20}

\x20\x20cout\x20<<\x20count\x20<<\x20endl;

\x20\x20return\x200; }

C++ 字符串删除空格:高效算法与代码示例

原文地址: https://www.cveoy.top/t/topic/p6Vg 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录