avatar
x*u
1
计算机程序设计公司,招聘C++以及网页设计员
公司已注册E-Verify,可以提供OPT和OPT延期服务
请发Resume到 4*******[email protected]
或电话联系: 646-535-8857
avatar
d*p
2
一个比较两个字符串的函数(相等返回0,否则-1或者1):
int compareStr(str1, str2)
if (length(str1) < length(str2))
return -1;
else if (length(str1) > length(str2))
return 1;
// two string equal in length
for each character c1i in str1 and c2i in str2
if c1i is upper case
convert c1i to lower case
if c2i is upper case
convert c2i to lower case
if (c1i < c2i)
avatar
x*u
3
可提供OPT延期服务~
avatar
y*e
4
直接用STL的equal呢?
inline bool eq_nocase(char c1, char c2) {
return toupper(c1) == toupper(c2);
}
const int N = strlen(str1);
equal(str1, str1+N, str2, eq_nocase);
avatar
X*r
5
为什么不用strcasecmp?

【在 d****p 的大作中提到】
: 一个比较两个字符串的函数(相等返回0,否则-1或者1):
: int compareStr(str1, str2)
: if (length(str1) < length(str2))
: return -1;
: else if (length(str1) > length(str2))
: return 1;
: // two string equal in length
: for each character c1i in str1 and c2i in str2
: if c1i is upper case
: convert c1i to lower case

avatar
d*p
6
The string comes as wstring, not string.
Do you see better algorithm? The function is called so many times since it
is used to compare keys in a lot of maps.

【在 X****r 的大作中提到】
: 为什么不用strcasecmp?
avatar
d*p
7
Thanks.
I guess this function is more general purposed and should be slower than the
code posted? Anyway I will do a profiling.

【在 y****e 的大作中提到】
: 直接用STL的equal呢?
: inline bool eq_nocase(char c1, char c2) {
: return toupper(c1) == toupper(c2);
: }
: const int N = strlen(str1);
: equal(str1, str1+N, str2, eq_nocase);

avatar
g*g
8
http://www.docjar.com/html/api/java/lang/String.java.html
你可以看看这个源码。

【在 d****p 的大作中提到】
: 一个比较两个字符串的函数(相等返回0,否则-1或者1):
: int compareStr(str1, str2)
: if (length(str1) < length(str2))
: return -1;
: else if (length(str1) > length(str2))
: return 1;
: // two string equal in length
: for each character c1i in str1 and c2i in str2
: if c1i is upper case
: convert c1i to lower case

avatar
X*r
9
wcscasecmp() if available
You can check the source code of strcasecmp to get the idea. However, it
really depends on the representation of the string, e.g. checking the
length first is not efficient for C strings, but it is for C++'s
std::string.

it

【在 d****p 的大作中提到】
: The string comes as wstring, not string.
: Do you see better algorithm? The function is called so many times since it
: is used to compare keys in a lot of maps.

avatar
d*p
11
Will try wcscasecmp - not sure it is available on all platforms.
If it doesn't work, I may consider building a lookup table to do the case
translation directly.

【在 X****r 的大作中提到】
: wcscasecmp() if available
: You can check the source code of strcasecmp to get the idea. However, it
: really depends on the representation of the string, e.g. checking the
: length first is not efficient for C strings, but it is for C++'s
: std::string.
:
: it

avatar
y*d
12
不能预先都转化为 upper case 的么?

【在 d****p 的大作中提到】
: The string comes as wstring, not string.
: Do you see better algorithm? The function is called so many times since it
: is used to compare keys in a lot of maps.

avatar
d*p
13
That's a good idea. However for now it is not realistic since project is
close to ending and such a change may be risky - too much code relying on
this little function. I would like to say if we were starting from scratch
again, this kind of preprocessing is absolutely necessary.
I tried using a char map to convert upper char to lower case char. Scaled
cpu time spent on the function dropped from 22 to 16, around 27% boost. ie
if (c >= L'A' && c < L'Z') c -= L'A' - L'a'
to
c = charmap[c] where ch

【在 y***d 的大作中提到】
: 不能预先都转化为 upper case 的么?
avatar
v*s
14
如果是C /C++, 前面2个strlen应该去掉,合并到后面的循环中。
具体细节自己考虑吧,或者google c strcmp 函数。

【在 d****p 的大作中提到】
: 一个比较两个字符串的函数(相等返回0,否则-1或者1):
: int compareStr(str1, str2)
: if (length(str1) < length(str2))
: return -1;
: else if (length(str1) > length(str2))
: return 1;
: // two string equal in length
: for each character c1i in str1 and c2i in str2
: if c1i is upper case
: convert c1i to lower case

avatar
r*t
15
有些标点符号比 upper case 大,所以不行。

【在 y***d 的大作中提到】
: 不能预先都转化为 upper case 的么?
avatar
r*t
16
转成 lower case 以后,wstring 有 operator- 没有?如果没有的话做成 union
{wstring, long long...},或者 cast 成足够大的整数,然后用整数相减行不行呢?或者
derive wstring 实现减法也行。

is
on
scratch
Scaled
boost.
ie
under

【在 d****p 的大作中提到】
: That's a good idea. However for now it is not realistic since project is
: close to ending and such a change may be risky - too much code relying on
: this little function. I would like to say if we were starting from scratch
: again, this kind of preprocessing is absolutely necessary.
: I tried using a char map to convert upper char to lower case char. Scaled
: cpu time spent on the function dropped from 22 to 16, around 27% boost. ie
: if (c >= L'A' && c < L'Z') c -= L'A' - L'a'
: to
: c = charmap[c] where ch

avatar
d*p
17
casting wstring to large integer is a very interesting concept.thanks.

?或者

【在 r****t 的大作中提到】
: 转成 lower case 以后,wstring 有 operator- 没有?如果没有的话做成 union
: {wstring, long long...},或者 cast 成足够大的整数,然后用整数相减行不行呢?或者
: derive wstring 实现减法也行。
:
: is
: on
: scratch
: Scaled
: boost.
: ie

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。