C++中有split这种操作吗?看下题:
c++吧
全部回复
仅看楼主
level 4
痛逝3载 楼主
比方说要读一行数据:mus|123|fef3|817u|fe|ufgo| ,其以“|”为分割线,要取出“|”中间的数据,用什么函数比较好
2013年03月12日 08点03分 1
level 8
这个用fopen不就能解决了啊?我新手,随便说说,你随便试试。
2013年03月12日 08点03分 2
while(fscanf(fp,"%d %c %d %c %d %c %d %c %d",&a,&punct,&b,&punct,&c,&punct,&d,&punct,&e)!=EOF)
2013年03月12日 08点03分
回复 74寂寞追风86 :这个可以是可以,不过可能太麻烦了。。。因为那一行数据只是源数据的10分之一。。。
2013年03月12日 08点03分
回复 痛逝3载 :如果后面的格式是一样的,会一直读取知道末尾的。另外说下,没有必要就不要使用freopen了,用了之后键盘不太好操作。
2013年03月12日 09点03分
level 10
python写多了斯基
2013年03月12日 08点03分 3
level 8
C++中的split 好像也是自己写的吧 最近看accelerated c++都是自己写的。只要改下空格为|应该就行了吧
2013年03月12日 09点03分 7
额。。。不知道怎么写。。。[汗]
2013年03月12日 11点03分
思路是用两个标志量确定字符的位置 再把这段字符串给容器
2013年03月12日 11点03分
level 1
■分离分隔符字符
▲主函数(cmain.cpp)
#include "jj.h"
int main()
{string a="aa+bb+cc+xx+yy+zz";int i,j;
for(i=1;i<=dec(a,"+")+1;i++)
cout<<dtxt(a, "+", i)<<endl;
cout<<endl;
a="dd";
for(i=1;i<=dec(a,"+")+1;i++)
cout<<dtxt(a, "+", i)<<endl;
cout<<endl;
a="dd bb cc ss";
for(i=1;i<=dec(a," ")+1;i++)
cout<<dtxt(a, " ", i)<<endl;
cout<<endl;
a="mm";
for(i=1;i<=dec(a," ")+1;i++)
cout<<dtxt(a, " ", i)<<endl;
system("pause");return 0;
}
▲函数头文件(jj.h)
#include <iostream>
#include <string>
using namespace std;
string mid1(string s,int a,int b);
string mid2(string s,int a,int b);
int defc(string s,string a,int n);
int dec(string s,string a);
int len(string s);
string dtxt(string s,string a,int n);
▲函数声明文件(jj.cpp)
#include "jj.h"
string mid1(string s,int a,int b)
{int i;string c;
for(i=a-1;i<=b-1;i++)
c=c+s[i];
return c;
}
string mid2(string s,int a,int b)
{int i;string c;
for(i=a-1;i<=a-1+b-1;i++)
c=c+s[i];
return c;
}
int defc(string s,string a,int n)
{int i,j=0;
for(i=1;i<=s.length()-a.length()+1;i++)
{if(mid2(s,i,a.length())==a) j++;
if(j==n) break;}
return i;
}
int dec(string s,string a)
{int i,j=0;
for(i=1;i<=s.length()-a.length()+1;i++)
{if(mid2(s,i,a.length())==a) j++;}
return j;
}
int len(string s)
{return (s.length());}
string dtxt(string s,string a,int n)
{int j;j=dec(s,a);string r;
if (n==1)
if (j!=0) r=mid1(s,1,defc(s,a,1)-1); else r=s;
else if (n==j+1)
r=mid1(s, defc(s, a, j) + 1, len(s));
else
r=mid1(s, defc(s, a, n-1) + 1, defc(s, a, n) - 1);
return r;
}
2013年03月12日 11点03分 8
level 1
2013年03月12日 11点03分 9
level 12
strtok
2013年03月12日 11点03分 10
level 15
[土文处理登记字]2013-051

11
L @永恒的条形码 在 2013-07-06 08:08 通过
回复挖坟,
已删除。
2013年07月06日 04点07分 12
level 5
你可以自己写一个Tokenize的类处理这种
Tokenize.h
#pragma once
class Tokenize
{
private:
char m_separator;
int m_count;
char** m_tokens;
const char* m_oristring;
public:
Tokenize(const char *oristring, const char separator);
int Count();
void Split();
int GetCount();
char ** GetTokens();
~Tokenize(void);
};
Tokenize.cpp
#include "StdAfx.h"
#include "Tokenize.h"
#include <cstring>
Tokenize::Tokenize(const char *oristring, const char separator)
{
m_separator = separator;
m_count = 0;
m_oristring = oristring;
m_tokens = NULL;
}
int Tokenize::Count() {
const char * curr = m_oristring;
while (curr != NULL && *curr)
{
while (*curr && *curr != m_separator)
{
++curr;
}
++m_count;
if (*curr)
++curr;
}
return m_count;
}
void Tokenize::Split() {
if (Count()) {
m_tokens = new char*[m_count];
const char *head, *tail;
char *temp;
head = tail = m_oristring;
int count = 0;
while (*tail)
{
while (*tail && *tail != m_separator)
{
++tail;
}
temp = new char[tail - head + 1];
memset(temp, 0, tail - head + 1);
memcpy(temp, head, tail - head);
m_tokens[count++] = temp;
if (*tail) {
++tail;
head = tail;
}
}
}
}
int Tokenize::GetCount() {
return m_count;
}
char ** Tokenize::GetTokens() {
return m_tokens;
}
Tokenize::~Tokenize(void)
{
for (int i = 0; i < m_count; ++i)
{
delete []m_tokens[i];
}
delete []m_tokens;
}
仅供参考
2016年02月08日 13点02分 16
level 5
2016年02月08日 13点02分 17
level 6
boost::string_algo
2016年02月09日 01点02分 19
1