各位高手帮帮我这个小菜鸟吧 提供些思路就行 要是能写出程序来就最好啦 先谢谢了
你可以把输入的字符都转化为小写或者大写,然后在比较排序!
/*---------------------------------------------------------------------------
File name: CaseInsensitiveCompare.cpp
Author: HJin (email: fish_sea_bird [at] yahoo [dot] com )
Created on: 8/23/2007 08:59:41
Environment: Windows XP Professional SP2 English +
Visual Studio 2005 v8.0.50727.762
Modification history:
===========================================================================
Problem statement:
---------------------------------------------------------------------------
http://bbs.bc-cn.net/viewthread.php?tid=165002
最近遇到一个小问题百思不得其解:从键盘输入几个字母(区分大小写),要求将这些字母按字典顺序(A~Z)排序。 比如输入了D c B a,则结果是:a B c D。
各位高手帮帮我这个小菜鸟吧 提供些思路就行 要是能写出程序来就最好啦 先谢谢了
Sample output:
---------------------------------------------------------------------------
DcBa
a B c D Press any key to continue . . .
BAba
A a B b Press any key to continue . . .
CaseInsensitiveCompare
a a C C e e e e I i i m n n o p r s s s t v Press any key to continue . . .
*/
#include <iostream>
#include <vector>
#include <iterator>
#include <algorithm>
using namespace std;
// functor for sort
struct CaseInsensitiveCompare
{
bool operator()(const char a, const char b)
{
return (a|0x20)<(b|0x20);
}
};
int main()
{
char s[1000];
cin>>s;
vector<char> vc(s, s+strlen(s));
sort(vc.begin(), vc.end(), CaseInsensitiveCompare());
copy(vc.begin(), vc.end(), ostream_iterator<char>(cout, " "));
return 0;
}