从c ++字符串中删除某些单词 [英] remove certain words from a c++ string

查看:130
本文介绍了从c ++字符串中删除某些单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


我想删除c ++字符串中的某些单词。单词列表是一个文件中的
,每个单词都在一个新行中。我尝试使用

std :: transform,但它有效。


任何人都知道我应该怎么做。


非常感谢,

Hp

Hi guys,

I want to remove certain words from a c++ string. The list of words are
in a file with each word in a new line. I tried using the
std::transform, but it dint work.

Anybody got a clue as to how i should go about this.

thanks a lot,
Hp

推荐答案

pr **************** @ gmail .com 写道:
大家好,

我想删除c ++字符串中的某些单词。单词列表在一个文件中,每个单词都在一个新行中。我尝试使用
std :: transform,但它有效。

任何人都知道我应该怎么做。

非常感谢,Hp
Hi guys,

I want to remove certain words from a c++ string. The list of words are
in a file with each word in a new line. I tried using the
std::transform, but it dint work.

Anybody got a clue as to how i should go about this.

thanks a lot,
Hp



尝试使用string :: find和string remove(我添加了swap for

优化,你不必) :


例如:


string str =" Hello world是我能做的唯一一项任务",

remword = world;

size_t = pos;


if((pos = str.find(remword))!= string :: npos)

{

str.swap(str.erase(pos,remword.length()));


}


ps。我统治了!


Try using string::find and string remove (I added swap for
optimization, you don''t have to):

example:

string str="Hello world is the only assignment I can do",
remword=world;
size_t=pos;

if((pos=str.find(remword))!=string::npos)
{
str.swap(str.erase(pos,remword.length()));

}

ps. I rule!


2005年10月22日16:33:04 -0700, pr **************** @ gmail.com 写道:
On 22 Oct 2005 16:33:04 -0700, pr****************@gmail.com wrote:
大家好,

我想删除c ++字符串中的某些单词。单词列表在一个文件中,每个单词都在一个新行中。我尝试使用
std :: transform,但它有效。

任何人都知道我应该怎么做。
Hi guys,

I want to remove certain words from a c++ string. The list of words are
in a file with each word in a new line. I tried using the
std::transform, but it dint work.

Anybody got a clue as to how i should go about this.



transform()不会删除容器中的条目,只会修改它们。


使用string :: find()查找子字符串,然后使用字符串:: erase()从字符串中删除

子串。


-dr



transform() doesn''t remove entries in a container, it only modifies them.

Use string::find() to find the substring, then use string::erase() to remove
the substring from the string.

-dr


非常感谢您的回复。但我发现在从字符串中删除

字之前,我需要将c ++字符串从

upper转换为小写。我实际上使用了transform()来执行这个

操作,这对于dint工作。

而且,在uppertolower案例转换后,我需要阅读

文件包含所有的停用词,每行一个,从转换后的字符串中删除




非常感谢,

Hp

Hi, Thanks a lot for your replies. But i fugured out that before the
word removal from the string, i need to convert the c++ string from
upper to lower case. I infact used the transform() to perform this
operation, which dint work.
And also, after the uppertolower case conversion, i need to read the
file containing all the stopwords, one in each line, to be removed from
the transformed string.

Thanks a lot in advance,
Hp


这篇关于从c ++字符串中删除某些单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆