用c语言实现find函数,在c ++中使用find_if()函数

//Another way to split strings

#include

#include

#include

#include

using std::endl;

using std::cout;

using std::cin;

using std::string;

using std::vector;

using std::istream;

istream& getWords(istream&, vector&);

string& removeDelimeters(string&);

bool space(char);

bool not_space(char);

void display(const vector&);

int main()

{

vector words;

getWords(cin,words);

display(words);

return 0;

}

void display(const vector& vec)

{

cout<

for(vector::const_iterator iter = vec.begin();iter != vec.end();iter++)

{

cout<

}

}

bool space(char c)

{

return isspace(c);

}

bool not_space(char c)

{

return !isspace(c);

}

string& removeDelimeters(string& word)

{

string delim = ",.`~!@#$%^&*()_+=-{}][:';?>

for(unsigned int i = 0;i

{

for(unsigned int j = 0;j

{

if(word[i] == delim[j])

{

word.erase(word.begin()+i); //removes the value at the given index

break;

}

}

}

return word;

}

istream& getWords(istream& in, vector& vec)

{

typedef string::const_iterator iter;

string initial;

cout<

getline(cin,initial);

initial = removeDelimeters(initial);

iter i = initial.begin();

while(i != initial.end())

{

//ignore leading blanks

i = find_if(i,initial.end(),not_space);

//find the end of the word

iter j = find_if(j,initial.end(),space);

//copy the characters in [i,j)

if(i != initial.end())

{

vec.push_back(string(i,j));

}

i = j;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值