怎样用algorithm中的算法查找string中不区分大小写'x'的位置???

share_feelings 2009-05-15 04:24:36
如果查找'x'的话,可以用
iter_x=std::find(str.begin(),str.end(),'x');
如果想查找x不区分大小写,怎么写呀????
...全文
397 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
maosher 2009-05-15
  • 打赏
  • 举报
回复
当然还可以写得再通用一些
maosher 2009-05-15
  • 打赏
  • 举报
回复


#include "stdafx.h"
#include <list>
#include <algorithm>


bool isXx( char c )
{
return (c == 'x' || c == 'X');
}

int main( )
{
using namespace std;

list <char> L;
list <char>::iterator Iter;
list <char>::iterator result;

L.push_back( 'a' );
L.push_back( 'x' );
L.push_back( 'b' );
L.push_back( 'X' );
L.push_back( 'c' );

cout << "L = ( " ;
for ( Iter = L.begin( ) ; Iter != L.end( ) ; Iter++ )
cout << *Iter << " ";
cout << ")" << endl;


result = find_if( L.begin( ), L.end( ), isXx );
if ( result == L.end( ) )
cout << "There is no element like x or X."
<< endl;
else
result++;
cout << "There is an element like x or X in list L,"
<< "\n and it is followed by a "
<< *(result) << "." << endl;

system("pause");
}

share_feelings 2009-05-15
  • 打赏
  • 举报
回复
谢谢各位
liuka 2009-05-15
  • 打赏
  • 举报
回复
struct cmpnocase
{
cmpnocase (char cT):C(cT){};
bool operator()(char a) const
{
if(a==tolower(C) || a == toupper(C))
return true;
else
return false;
}
char C
};
std::find_if(str.begin(), str.end(), cmpnocase('X'));
share_feelings 2009-05-15
  • 打赏
  • 举报
回复
operator()是类的返回值吗?????
huizhouxueyuan 2009-05-15
  • 打赏
  • 举报
回复
#include<iostream>
#include<vector>
#include<algorithm>

using namespace std;

// 自己写的predicate
class ignoreCase
{
public:
ignoreCase(char t)
: target(t)
{
};
bool operator()(char t)
{
if(toupper(t) == toupper(target)) //忽略大小写
{
return true;
}
return false;
}
private:
char target;
};

int main()
{
vector<char> vec;
for(char a = 'A'; a <= 'z'; ++a)
{
vec.push_back(a);
}
vector<char>::iterator pos = find_if(vec.begin(), vec.end(),ignoreCase('a'));

cout << *pos << endl;

return 0;
}
share_feelings 2009-05-15
  • 打赏
  • 举报
回复
8楼的方法就是我想要的,谢谢。。
lingyin55 2009-05-15
  • 打赏
  • 举报
回复
用个临时变量

[Quote=引用 9 楼 share_feelings 的回复:]
这种方法我也想过,但是str是const类型。
传进来的参数str不想改变它原来的存储。
[/Quote]
share_feelings 2009-05-15
  • 打赏
  • 举报
回复
这种方法我也想过,但是str是const类型。
传进来的参数str不想改变它原来的存储。
shantouman 2009-05-15
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 liuka 的回复:]
自己写个仿函数比较
[/Quote]
template<char C>
struct cmpnocase
{
bool operator()(char a) const
{
return tolower(C) == tolower(a);
}
};
std::find_if(str.begin(), str.end(), cmpnocase<'x'>());
这样应该可以吧

lingyin55 2009-05-15
  • 打赏
  • 举报
回复
或者用tolower,toupper,名字记错了,不好意思。
其实c++ primer里面有一章文本处理的说了很多字符串处理的,
楼主可以取看看:)

[Quote=引用 5 楼 lingyin55 的回复:]
有现成的函数可以调用啊,应该是upper吧
upper(str.c_str());

引用 3 楼 share_feelings 的回复:
2楼说详细点,我也是这个想法。

但是初学STL,不太会。。
[/Quote]
lingyin55 2009-05-15
  • 打赏
  • 举报
回复
#include<string.h>

strupr(str); //将小写转换成大写

strlwr(str); //将大写转换成小写
lingyin55 2009-05-15
  • 打赏
  • 举报
回复
有现成的函数可以调用啊,应该是upper吧
upper(str.c_str());

[Quote=引用 3 楼 share_feelings 的回复:]
2楼说详细点,我也是这个想法。

但是初学STL,不太会。。
[/Quote]
maosher 2009-05-15
  • 打赏
  • 举报
回复
如果目标文件很大呢,要全转换为大写再找是不有点费劲
share_feelings 2009-05-15
  • 打赏
  • 举报
回复
2楼说详细点,我也是这个想法。

但是初学STL,不太会。。
lingyin55 2009-05-15
  • 打赏
  • 举报
回复
先把要查找的字符串全部转为小写或大写

[Quote=引用楼主 share_feelings 的帖子:]
如果查找'x'的话,可以用
iter_x=std::find(str.begin(),str.end(),'x');
如果想查找x不区分大小写,怎么写呀????
[/Quote]
liuka 2009-05-15
  • 打赏
  • 举报
回复
自己写个仿函数比较

65,210

社区成员

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

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