c++ string 编写下面程序

tmwanly 2012-09-23 06:22:21

张三|3456123, 湖南
李四,4564234| 湖北
王小二, 4433253|北京
...


以 “|” “,”为分隔符,同时又要过滤空格,把每行分成相应的字段。
通过运用string里面的内容编写。
...全文
147 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
tmwanly 2012-09-24
  • 打赏
  • 举报
回复
这个还是没有达到过滤空格的目的啊,而是把空格也作为分隔符了[Quote=引用 6 楼 的回复:]
C/C++ code

#include <iostream> // 数据流输入/输出
#include <string> // 字符串类
#include <algorithm> // STL 通用算法

using namespace std;

int main()
{

string str = "张三 | 3456123 , 湖南";

size_t……
[/Quote]
tmwanly 2012-09-24
  • 打赏
  • 举报
回复
楼上的大哥辛苦了,不过感觉太麻烦,而且运行后也有问题,虽然你辛苦了但是不能给你分不好意思啊。

下面是我写的,有木有高手指点过滤掉空格
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
string strinf1="张山|3456123,湖南 李四,4564234| 湖北 王小二,4433253|北京,";
index = 0;
while(strinf1[index] != '\0') {
if(strinf1[index] != '|'&&strinf1[index]!=',') {
cout << strinf1[index];
}
else {
cout << endl;
}
index++;
}

return 0;
}
hongwenjun 2012-09-23
  • 打赏
  • 举报
回复
#include <iostream>    // 数据流输入/输出
#include <string> // 字符串类
#include <algorithm> // STL 通用算法
#include <vector>

using namespace std;
// 使用value中字符,切割str字符串,存到 strtok_vec容器里
vector<string>& stringtok(const string& str , const string& value , vector<string>& strtok_vec);
int main()
{

string str = " | 张三 | 3456123 , 湖南 | ";

size_t pos = 0, posend = 0;

// pos = str.find_first_not_of("| ," , 0); // 搜索第一个,其实可以合并到下面 while里
// posend = str.find_first_of("| ," , 0);
// cout << str.substr(pos, posend) << endl;

while ((pos = str.find_first_not_of("| ," , posend)) != string::npos) {
posend = str.find_first_of("| ," , pos);
cout << str.substr(pos, posend - pos) << endl; // substr 第二个参数不是pos,是个长度
}

string text = "张三|3456123, 湖南\n"
"李四,4564234| 湖北\n"
"王小二, 4433253|北京\n";

vector<string> IDTable;
stringtok(text , "| ,\n" , IDTable);
return 0;
}


/* find_first_of() 查找第一个与value中的某值相等的字符
* find_first_not_of() 查找第一个与value中的所有值都不相等的字符
* find_last_of() 查找最后一个与value中的某值相等的字符
* find_last_not_of() 查找最后一个与value中的所有值都不相等的字符
*/

/* basic_string substr( size_type index, size_type num = npos );
* substr()返回本字符串的一个子串,从index开始,长num个字符。
* 如果没有指定,将是默认值 string::npos。
* 这样,substr()函数将简单的返回从index开始的剩余的字符串。
*/


vector<string>& stringtok(const string& str , const string& value , vector<string>& strtok_vec)
{
size_t pos = 0, posend = 0;
while ((pos = str.find_first_not_of("| ," , posend)) != string::npos) {
posend = str.find_first_of("| ," , pos);
cout << str.substr(pos, posend - pos) << endl;
strtok_vec.push_back(str.substr(pos, posend - pos));
}
return strtok_vec;
}

hongwenjun 2012-09-23
  • 打赏
  • 举报
回复

#include <iostream> // 数据流输入/输出
#include <string> // 字符串类
#include <algorithm> // STL 通用算法

using namespace std;

int main()
{

string str = "张三 | 3456123 , 湖南";

size_t pos = 0, posend = 0;

// pos = str.find_first_not_of("| ," , 0); // 搜索第一个,其实可以合并到下面 while里
// posend = str.find_first_of("| ," , 0);
// cout << str.substr(pos, posend) << endl;

while ((pos = str.find_first_not_of("| ," , posend)) != string::npos) {
posend = str.find_first_of("| ," , pos);
cout << str.substr(pos, posend - pos) << endl; // substr 第二个参数不是pos,是个长度
}

return 0;
}


/* find_first_of() 查找第一个与value中的某值相等的字符
* find_first_not_of() 查找第一个与value中的所有值都不相等的字符
* find_last_of() 查找最后一个与value中的某值相等的字符
* find_last_not_of() 查找最后一个与value中的所有值都不相等的字符
*/



改好上面代码
shuweiya 2012-09-23
  • 打赏
  • 举报
回复
路过帮忙,不能第四贴
hongwenjun 2012-09-23
  • 打赏
  • 举报
回复
#include <iostream>    // 数据流输入/输出
#include <string> // 字符串类
#include <algorithm> // STL 通用算法

using namespace std;

int main()
{

string str = "张三 | 3456123 , 湖南";

size_t pos =0, posend =0;

// pos = str.find_first_not_of("| ," , 0); // 搜索第一个,其实可以合并到下面 while里
// posend = str.find_first_of("| ," , 0);
// cout << str.substr(pos, posend) << endl;

while ((pos = str.find_first_not_of("| ," , posend)) != string::npos) {
posend = str.find_first_of("| ," , pos);
cout << str.substr(pos, posend) << endl;
}

return 0;
}


/* find_first_of() 查找第一个与value中的某值相等的字符
* find_first_not_of() 查找第一个与value中的所有值都不相等的字符
* find_last_of() 查找最后一个与value中的某值相等的字符
* find_last_not_of() 查找最后一个与value中的所有值都不相等的字符
*/



这个实现,不能找出中间的,不知道哪里出问题了
hongwenjun 2012-09-23
  • 打赏
  • 举报
回复
find_first_of() 查找第一个与value中的某值相等的字符
find_first_not_of() 查找第一个与value中的所有值都不相等的字符
find_last_of() 查找最后一个与value中的某值相等的字符
find_last_not_of() 查找最后一个与value中的所有值都不相等的字符

使用这些算法 组合一下好像也可以
hongwenjun 2012-09-23
  • 打赏
  • 举报
回复
使用 C 的库简单点

好像string 没有这样的库
你哪个先 每行 复制到 一个char str[max_len] 里,然后切割一下就可以了
#include <stdio.h>
#include <string.h>
int main()
{
char str[] = "张三|3456123, 湖南";
char delims[] = "|, ";
char* result = NULL;

result = strtok(str, delims);
while (result != NULL) {
printf("result is \"%s\"\n", result);
result = strtok(NULL, delims);
}

}

/* char* strtok(char* str1, const char* str2);
* 功能:函数返回字符串str1中紧接“标记”的部分的指针,
* 字符串str2是作为标记的分隔符。如果分隔标记没有找到,
* 函数返回NULL。为了将字符串转换成标记,
* 第一次调用str1 指向作为标记的分隔符。之后所以的调用str1 都应为NULL。
*/
傻X 2012-09-23
  • 打赏
  • 举报
回复
While+(Find+SubStr)组合。

Find的时候根据要2次(|和,),判断谁的索引小,就用谁。

64,639

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧