请问istringstream 如何自定义分界字符(默认是\n)

hittyo 2011-05-04 03:44:22
请问istringstream 如何自定义分界字符(默认是\n)

目的是为了从istringstream有效地截取字符子串。
...全文
587 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
hittyo 2011-05-04
  • 打赏
  • 举报
回复
谢谢了,楼上这个牛。。。
pengzhixi 2011-05-04
  • 打赏
  • 举报
回复
int main()
{
int length ;
string st="Enter,the,name,of,an,existing,text,file:";
istringstream stream(st);
int i=0;
char array[20]={0};
while(stream.get(array,20,','))
{
length = stream.tellg();
cout<<array<<endl;
stream.seekg (length+1, ios::beg);
}

system("pause");
return 0;
}
给你一个马马虎虎的吧
hittyo 2011-05-04
  • 打赏
  • 举报
回复
好像我的标题没说清楚,我说的'\n'是我已知的,现在我想用','分界。。。


有点类似 get方法
ss.get(char *,size_t n,',')
但这个方法碰到空格就自动断了,很奇怪怎么就没有自定义了
ljt3969636 2011-05-04
  • 打赏
  • 举报
回复


#include <sstream>
string str="125 320 512 750 333";


istringstream ss(str);
strvec str1;
string str2;

while(ss >> str2)
{
str1.push_back(str2);
}

copy(str1.begin(),str1.end(),ostream_iterator<string>( cout,"\n"));

無_1024 2011-05-04
  • 打赏
  • 举报
回复
学习一下 这个我一直不怎么喜欢用
就想叫yoko 2011-05-04
  • 打赏
  • 举报
回复
#include <string.h>
#include <stdio.h>

char string[] = "A string\tof ,,tokens\nand some more tokens";
char seps[] = " ,\t\n";
char *token;

int main( void )
{
printf( "Tokens:\n" );

// Establish string and get the first token:
token = strtok( string, seps ); // C4996
// Note: strtok is deprecated; consider using strtok_s instead
while( token != NULL )
{
// While there are tokens in "string"
printf( " %s\n", token );

// Get next token:
token = strtok( NULL, seps ); // C4996
}
}
老邓 2011-05-04
  • 打赏
  • 举报
回复
看起来只能用boost::tokenizer了。
#include <iostream>
#include <boost/tokenizer.hpp>
#include <string>
int main()
{
std::string str = ";;Hello|world||-foo--bar;yow;baz|";
typedef boost::tokenizer<boost::char_separator<char> > tokenizer;
boost::char_separator<char> sep("-;|");
tokenizer tokens(str, sep);
for (tokenizer::iterator tok_iter = tokens.begin(); tok_iter != tokens.end(); ++tok_iter)
std::cout << "<" << *tok_iter << "> ";
std::cout << "\n";
return EXIT_SUCCESS;
}

或者用CString.
老邓 2011-05-04
  • 打赏
  • 举报
回复
查了一遍文档:没有自定义分隔符功能。
http://www.cplusplus.com/reference/iostream/istringstream/
pengzhixi 2011-05-04
  • 打赏
  • 举报
回复
istringstream 是不能根据你指定的分隔符来切割字符串的。但是ostream_iterator也不是像你说的那样,只是它会多输出你指定的那个符号。
pengzhixi 2011-05-04
  • 打赏
  • 举报
回复
额 实现没有指定。如果你想指定,那么用ostream_iterator.
老邓 2011-05-04
  • 打赏
  • 举报
回复
#include <iostream>
#include <sstream>
#include <string>
using namespace std;

int main()
{
int n, val;
string stringvalues;

stringvalues = "125 320 512 750 333";
istringstream iss(stringvalues, istringstream::in);

for (n = 0; n < 5; n++)
{
iss >> val;
cout << val * 2 << endl;
}

return 0;
}
老邓 2011-05-04
  • 打赏
  • 举报
回复
空格、回车、换行都是默认的分界符吧。

64,701

社区成员

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

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