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

问题描述

我有需要规范化的数据 - 它是名称数据。例如,我

具有以下内容:Watts,J.C。我希望(1)解析名字

(J.C。)并将其调整为JC。基本上,我想从名字中删除

标点字符。 substring。

我已经查看了C ++中的basic_string,但我找不到

函数,它们将_remove"一个值的字符。有一些

运算符有助于找到我想要移除的角色的位置,但我看不出实际上是怎么做的。请求建议。

TIA

I have data I need to normalize - it''s "name" data. For example, I
have the following: "Watts, J.C." I wish to (1) parse the "first name"
("J.C.") and adjust it to "JC". Essentially, I want to remove the
punctuation characters from the "first name" substring.
I''ve looked at the basic_string in C++, but I can''t find the
functions that will _remove" character(s) from a value. There are
operators that are helpful in finding the position of the character I
wish to remove, but I can''t see how to actually do is. Pleas advise.
TIA

推荐答案



std :: basic_string :: erase应该可以解决问题。您可能需要首先找到

字符串中的字符...


V

-

请在通过电子邮件回复时删除资金''A'

我没有回复最热门的回复,请不要问

std::basic_string::erase should do the trick. You may need to locate
the characters in the string first...

V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask




不完美但可以给你一个开始;)


#include< cstdlib>

#include< iostream>

#include< list>

#include< map>

#include< string>


使用命名空间std;


int main()

{

string str(" Watts,JC");

字符串标点符号(&.;;:"); //可以添加更多

string :: size_type index_punctuation,index = str.find_last_of(",");

string name(str.substr(index + 1) ,string :: npos));

cout<<名称<< (b
!= string :: npos)

name.erase( index_punctuation,1);

cout<<名称<<结束;

返回0;

}


Rgds,

Not perfect but can give you a start ;)

#include <cstdlib>
#include <iostream>
#include <list>
#include <map>
#include <string>

using namespace std;

int main()
{
string str("Watts, J.C.");
string punctuation(".;:"); // can add more
string::size_type index_punctuation, index = str.find_last_of(",");
string name(str.substr(index+1,string::npos));
cout << name << endl;
while((index_punctuation=name.find_first_of(punctu ation))
!=string::npos)
name.erase(index_punctuation,1);
cout << name << endl;
return 0;
}

Rgds,




您列出了两个操作...


//操作1

对< string,string parse_name(const string& s)

{

const string :: size_type commaPos = s.find('','');

assert(commaPos!= string :: npos);

const string :: size_type firstNamePos =

s.find_first_not_of("",commaPos + 1) ;

返回make_pair(s.substr(firstNamePos),s.substr(0,commaPos));

}


//操作2

如果您只想删除句点,那么只需:


name.erase(remove(name.begin(),name。 end(),''。''),name.end());


或者如果你不想修改原文......


string adjustedName;

remove_copy(name.begin(),name.end(),back_inserter(adjustedName),

''。'' );


如果你想删除其他角色,那么你需要一些东西

喜欢:


struct char_found_in

{

string text;

char_fou nd_in(const string& s):text(s){}

bool operator()(string :: value_type c)const {

return find(text.begin( ),text.end(),c)!= text.end();

}

};


string removeArbitrary(const string& s,const string& chars)

{

字符串结果;

remove_copy_if(s.begin(),s.end(),back_inserter(result),

char_found_in(chars));

返回结果;

}

You have two operations listed...

// operation 1
pair< string, string parse_name( const string& s )
{
const string::size_type commaPos = s.find( '','' );
assert( commaPos != string::npos );
const string::size_type firstNamePos =
s.find_first_not_of( " ", commaPos + 1 );
return make_pair( s.substr( firstNamePos ), s.substr( 0, commaPos ) );
}

// operation 2
If all you want to do is remove periods then simply:

name.erase( remove( name.begin(), name.end(), ''.'' ), name.end() );

or if you don''t want to modify the original...

string adjustedName;
remove_copy( name.begin(), name.end(), back_inserter( adjustedName ),
''.'' );

If you want to remove other characters as well, then you need something
like:

struct char_found_in
{
string text;
char_found_in( const string& s ): text( s ) { }
bool operator()( string::value_type c ) const {
return find( text.begin(), text.end(), c ) != text.end();
}
};

string removeArbitrary( const string& s, const string& chars )
{
string result;
remove_copy_if( s.begin(), s.end(), back_inserter( result ),
char_found_in( chars ) );
return result;
}


这篇关于从basic_string中删除字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 12:14