不得不留意的STL string重载函数和隐式类型转换

本文详细分析了STLstring的构造函数性能,并提供了代码测试结果,帮助开发者更好地理解并高效地使用STLstring。重点讨论了性能对比、字符串拷贝、比较函数、函数重载及误用场景,特别强调了字符串常量的表示方式对性能的影响。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在360云引擎技术博客的“深入剖析linux GCC 4.4的STL string”这篇blog的指导下,看了一些STL string的实现代码,并针对我们平时对string的一些常规用法做了一些测试。这里做一下总结,希望能帮助大家更好的理解理解STL string,更高效的使用STL string。

由于本文涉及到性能对比,接下来会有一些测试程序,所以首先看一下我们的测试环境:

1
2
3
4
$ uname -sr
Linux 2.6.32-220.23.1.tb750.el5.x86_64
$ gcc --version
gcc (GCC) 4.1.2 20080704 (Red Hat 4.1.2-51)

构造函数 std::string::string

函数声明

1
2
3
4
5
6
7
string ( );
string ( const string& str );
string ( const string& str, size_t pos, size_t n = npos );
string ( const char * s, size_t n );
string ( const char * s );
string ( size_t n, char c );
template < class InputIterator> string (InputIterator begin, InputIterator end);

我们对这些构造函数分别做下测试

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/* FILE: string_construct.cpp */
 
#include <stdio.h>
#include <string>
#include <sys/time.h>
 
int64_t getCurrentTime()
{
     struct timeval tval;
     gettimeofday(&tval, NULL);
     return (tval.tv_sec * 1000000LL + tval.tv_usec);
}
 
int main( int argc, char *argv[])
{
     const int loop = 10000;
 
     std::string source;
     source.resize(10240, '1' );
 
     int64_t start = 0;
     int64_t end = 0;
 
     start = getCurrentTime();
     for ( int i = 0; i < loop; ++i)
     {
         std::string str(source);
     }
     end = getCurrentTime();
     printf ( "call %-35s %d times: %ld us\n" ,
            "string(const string &)" , loop, (end - start));
 
     start = getCurrentTime();
     for ( int i = 0; i < loop; ++i)
     {
         std::string str(source, 0);
     }
     end = getCurrentTime();
     printf ( "call %-35s %d times: %ld us\n" ,
            "string(const string &, size_t)" , loop, (end - start));
 
     start = getCurrentTime();
     for ( int i = 0; i < loop; ++i)
     {
         std::string str(source.c_str());
     }
     end = getCurrentTime();
     printf ( "call %-35s %d times: %ld us\n" ,
            "string(const char *)" , loop, (end - start));
 
     start = getCurrentTime();
     for ( int i = 0; i < loop; ++i)
     {
         std::string str(source.c_str(), source.size());
     }
     end = getCurrentTime();
     printf ( "call %-35s %d times: %ld us\n" ,
            "string(const char *, size_t)" , loop, (end - start));
 
     start = getCurrentTime();
     for ( int i = 0; i < loop; ++i)
     {
         std::string str(source.size(), source.at(0));
     }
     end = getCurrentTime();
     printf ( "call %-35s %d times: %ld us\n" ,
            "string(size_t, char)" , loop, (end - start));
 
     start = getCurrentTime();
     for ( int i = 0; i < loop; ++i)
     {
         std::string str(source.begin(), source.end());
     }
     end = getCurrentTime();
     printf ( "call %-35s %d times: %ld us\n" ,
            "string(Iterator, Iterator)" , loop, (end - start));
 
     return 0;
}

在运行这段测试代码前,你可以尝试分析一下这几个函数调用,哪个耗时最长,哪个耗时最短呢?
……

再来看看编译运行的结果:

1
2
3
4
5
6
7
8
$ g++ -Wall -O2 -o string_construct string_construct.cpp
$ . /string_construct
call string(const string &)              10000 times : 528 us
call string(const string &, size_t)      10000 times : 9064 us
call string(const char *)                10000 times : 30021 us
call string(const char *, size_t)        10000 times : 9092 us
call string(size_t, char)                10000 times : 5719 us
call string(Iterator, Iterator)          10000 times : 8996 us

这里输出的结果跟你的预期一样么?我想只要仔细想想,应该都能预料到这个结果:

  • string(const string &)

毫无疑问它是最快的,因为string的COW(copy-on-write)特性。至于COW就不在本文讨论范围了,感兴趣的朋友可以参考《深入剖析 linux GCC 4.4 的 STL string

  • string(const string &, size_t)
  • string(const char *, size_t)
  • string(size_t, char)
  • string(Iterator, Iterator)

这几个差别不大,都需要分配一块内存,然后将source里的内容拷贝过去

  • string(const char *)

它耗时非常大,这个我想应该会出乎部分人的意料:平时咱们很多时候就是这么用的啊?!它跟string(const char *, size_t)只是差了一个参数而已!但是要注意,这个参数是前面的字符串的长度!如果没有这个长度,那就不能确定需要分配的内存的大小,只能先计算一遍长度。可以通过stl的源码(/usr/include/c++/4.1.2/bits/basic_string.tcc)来确认下,这里摘取相关部分如下:

1
2
3
4
5
6
7
8
9
10
11
12
template < typename _CharT, typename _Traits, typename _Alloc>
   basic_string<_CharT, _Traits, _Alloc>::
   basic_string( const _CharT* __s, size_type __n, const _Alloc& __a)
   : _M_dataplus(_S_construct(__s, __s + __n, __a), __a)
   { }
 
template < typename _CharT, typename _Traits, typename _Alloc>
   basic_string<_CharT, _Traits, _Alloc>::
   basic_string( const _CharT* __s, const _Alloc& __a)
   : _M_dataplus(_S_construct(__s, __s ? __s + traits_type::length(__s) :
                  __s + npos, __a), __a)
   { }

通过这两个函数的实现代码可以看出,唯一的差别就在于 traits_type::length(__s)。traits_type::length的实现在/usr/include/c++/4.1.2/bits/char_traits.h,说简单一点就是于对string,直接调用strlen,对于wstring,则调用wcslen,这里就不详细介绍了。

小结

  1. 在做string的拷贝时,尽量使用string(const string &)的形式,以享受COW带来的好处
  2. 当需要使用char *构造一个string时,如果你已经知道这个char *表示的字符串长度,记得把这个长度一起传给string的构造函数
  3. 我想不会有人傻到用string str(source.c_str(), source.size())这种方式拷贝一个string吧?当然上面测试代码只是为了便于理解所以采用了这种形式

顺便提一下,std::string::assign跟std::string的构造函数类似,测试结果也基本一样。string& assign(const string &)跟string(const string &)一样也有copy-on-write。

1
2
3
4
5
6
string& assign ( const string& str );
string& assign ( const string& str, size_t pos, size_t n );
string& assign ( const char * s, size_t n );
string& assign ( const char * s );
string& assign ( size_t n, char c );
template < class InputIterator> string& assign ( InputIterator first, InputIterator last );

比较函数 std::string::compare

函数声明

1
2
3
4
5
6
int compare ( const string& str ) const ;
int compare ( const char * s ) const ;
int compare ( size_t pos1, size_t n1, const string& str ) const ;
int compare ( size_t pos1, size_t n1, const char * s) const ;
int compare ( size_t pos1, size_t n1, const string& str, size_t pos2, size_t n2 ) const ;
int compare ( size_t pos1, size_t n1, const char * s, size_t n2) const ;

对于compare函数,这里同样有一段测试代码,一起来看下:

在这段测试程序里,我们有4个比较测试函数,各个函数都将参数传进来的string跟自已预先定义好的一个字符串做比较。函数声明里加上noinline的attribute是为了防止编译器把compareUseStrcmp这个函数调用优化掉
同样,先尝试分析下运行这个测试程序会有什么样的结果?再来编译运行:

1
2
3
4
5
6
$ g++ -Wall -O2 -o string_compare string_compare.cpp
$ . /string_compare
compare with static string     10000 times : 2399us
compare with const string      10000 times : 4156us
compare with c-string          10000 times : 2625us
compare use strcmp             10000 times : 2388us

先来分析compare with static string和compare with const string这两个case,结果很明显,compareWithConstString这个函数里,每次函数调用都会在栈上构造一个foo临时对象,调用10000次就有10000次的构造和析构,当然很耗时了。所以,我们应该尽量用static const string表示字符串常量,而不是const string
再来对比compare with static string和compare with c-string,它俩为什么还差了近3ms的时间呢?我们去源码里寻找答案:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
template < typename _CharT, typename _Traits, typename _Alloc>
   int
   basic_string<_CharT, _Traits, _Alloc>::
   compare( const _CharT* __s) const
   {
     __glibcxx_requires_string(__s);
     const size_type __size = this ->size();
     const size_type __osize = traits_type::length(__s);
     const size_type __len = std::min(__size, __osize);
     int __r = traits_type::compare(_M_data(), __s, __len);
     if (!__r)
   __r = __size - __osize;
     return __r;
   }

看到了吗,这里又有个traits_type::length!所以,我们应该尽量用static const string表示字符串常量,而不是const string,也不是const char *。
compare with static string跟compare use strcmp结果差不多,我们就不分析了。

至于std::string的几个比较运算符(operator==,operator!=,operator>,operator<,operator>=,operator<=)都是直接调用的std::string::compare。我们也不多做分析了。

std::string里还有几个类似的函数重载,列举如下,我们在使用的时候都需要注意同样的问题:
std::string::find

1
2
3
4
size_t find ( const string& str, size_t pos = 0 ) const ;
size_t find ( const char * s, size_t pos, size_t n ) const ;
size_t find ( const char * s, size_t pos = 0 ) const ;
size_t find ( char c, size_t pos = 0 ) const ;

std::string::append

1
2
3
4
5
6
string& append ( const string& str );
string& append ( const string& str, size_t pos, size_t n );
string& append ( const char * s, size_t n );
string& append ( const char * s );
string& append ( size_t n, char c );
template < class InputIterator> string& append ( InputIterator first, InputIterator last );

std::string::replace

1
2
3
4
5
6
7
8
9
10
string& replace ( size_t pos1, size_t n1, const string& str );
string& replace ( iterator i1, iterator i2, const string& str );
string& replace ( size_t pos1, size_t n1, const string& str, size_t pos2, size_t n2 );
string& replace ( size_t pos1, size_t n1, const char * s, size_t n2 );
string& replace ( iterator i1, iterator i2, const char * s, size_t n2 );
string& replace ( size_t pos1, size_t n1, const char * s );
string& replace ( iterator i1, iterator i2, const char * s );
string& replace ( size_t pos1, size_t n1, size_t n2, char c );
string& replace ( iterator i1, iterator i2, size_t n2, char c );
template < class InputIterator> string& replace ( iterator i1, iterator i2, InputIterator j1, InputIterator j2 );

隐式类型转换

前面的测试结果表明,当STLstring有提供std::string作为参数的函数重载时,应该尽量使用std::string作为参数。接下来我们再看另外一个case:

这里我们主要测的是std::string作为std::map的key时,判断指定的key是否在map里,也就是std::map::find函数。那么我们先来看看std::map的find函数原型:

1
2
iterator find ( const key_type& k);
const_iterator find ( const key_type& k) const ;

这里find函数的参数是const key_type &k,在我们的测试程序里也就是const std::string &k。也就是说find函数只能接受std::string作为参数,但是在我们的测试程序里将一个字符串常量”key1″传给了find函数。这里就涉及到了隐式类型转换,将”key1″转换成一个临时的std::string对象。显然,这里的隐式类型转换需要构造一个临时的std::string对象,需要消耗时间。那我们看一下测试结果:

1
2
3
4
$ g++ -O2 -o implicit_conversion implicit_conversion.cpp
$ . /implicit_conversion
call map[const char *]                   10000 times : 4653 us
call map[const std::string &]            10000 times : 2441 us

结果很明显,正如我们分析的:隐式类型转换消耗了不少时间。
所以,在我们的代码里,应该尽量使用static const std::string的形式来表示字符串常量!

几种典型的误用场景

构造函数

1
2
3
4
5
6
7
8
9
10
11
12
<pre>std::string decode( const std::string &str);
std::string decode( const char *str)
{
     decode(std::string(str));
}
 
void test()
{
     std::string input;
     std::cin >> input;
     std::output = decode(input.c_str());
}

比较函数

1
2
3
4
5
6
7
8
9
10
11
12
std::string command;
std::cin >> command;
                                                       // static const std::string get = "get";
                                                       // static const std::string set = "set";
if (command == "get" )                   // if (str == get)
{
   printf ( "get command\n" );
}
else if (command == "set" )             // if (str == set)
{
   printf (set command\n";
}

字符串拼接

1
2
3
4
5
6
7
8
9
10
11
std::string keyword, properties;
std::cin >> keyword >> properties;
 
std::string url;
url += "/bin/search?" ;        // static const std::string prefix = "/bin/search?"; url += prefix;
url += "q=" ;                       // static const std::string q_key = "q="; url += qkey;
url += keyword;
url += "&" ;                         // url += '&';
url += "properties=" ;         // static const std::string properties_key = "properties="; url += properties;
url += properties;
url += "&outfmt=json"       // static const std::string outfmt = "&outfmt=json"; url += outfmt;

隐式类型转换

1
2
3
4
5
6
7
8
9
std::map<std::string, std::string> m;
m[ "key1" ] = "value1" ;                                   //
                                                                    // static const std::string key1 = "key1";
                                                                    // static const std::string value1 = "value1";
                                                                    // m[key1] = value1;
m[ "key2" ] = "value2" ;
const std::string &v1 = m[ "key1" ];               // const std::string &v1 = m[key1];
const std::string &v2 = m[ "key2" ];

这几段代码看着眼熟么?赶紧改掉吧 :)

转载于:https://www.cnblogs.com/gaiwen/articles/3006587.html

【为什么还需要学习C++?】 你是否接触很多语言,但从来没有了解过编程语言的本质?你是否想成为一名资深开发人员,想开发别人做不了的高性能程序?你是否经常想要窥探大型企业级开发工程的思路,但苦于没有基础只能望洋兴叹? 那么C++就是你个人能力提升,职业之路进阶的不二之选。【课程特色】 1.课程共19大章节,239课时内容,涵盖数据结构、函数、类、指针、标准库全部知识体系。2.带你从知识与思想的层面从0构建C++知识框架,分析大型项目实践思路,为你打下坚实的基础。3.李宁老师结合4大国外顶级C++著作的精华为大家推出的《征服C++11》课程。【学完后我将达到什么水平?】 1.对C++的各个知识能够熟练配置、开发、部署;2.吊打一切关于C++的笔试面试题;3.面向物联网的“嵌入式”面向大型化的“分布式”开发,掌握职业钥匙,把握行业先机。【面向人群】 1.希望一站式快速入门的C++初学者; 2.希望快速学习 C++、掌握编程要义、修炼内功的开发者; 3.有志于挑战更高级的开发项目,成为资深开发的工程师。 【课程设计】 本课程包含3大模块基础篇本篇主要讲解c++的基础概念,包含数据类型、运算符等基本语法,数组、指针、字符串等基本词法,循环、函数、类等基本句法等。进阶篇本篇主要讲解编程中常用的一些技能,包含类的高级技术、类的继承、编译链接命名空间等。提升篇:本篇可以帮助学员更加高效的进行c++开发,其中包含类型转换、文件操作、异常处理、代码重用等内容。
<think>好的,用户现在问的是关于C++ STL中max函数的用法。之前他们问过max_element的用法,现在转向max函数,可能需要区分两者的不同。首先,我需要确认用户是否清楚maxmax_element的区别,可能他们混淆了这两个函数。 接下来,我需要回忆std::max的几种重载形式。比如,它可以接受两个值,或一个initializer_list,或者比较函数作为参数。用户可能需要基本的用法,或者更高级的自定义比较方法。 然后,考虑用户可能的应用场景。如果他们是新手,可能需要简单明了的例子,比如比较两个数,或者多个数的情况。如果他们之前用过max_element,可能需要强调max用于直接比较值,而max_element用于容器中的元素。 还需要注意安全性问题,比如当使用initializer_list时,如果列表为空,会导致未定义行为。这时候可能需要提醒用户检查是否为空,但根据C++标准,这种情况是未定义的,所以应该告诉用户要避免传入空列表。 另外,用户可能会需要自定义类型的比较,这时候需要提到如何传递自定义比较函数或者重载operator<。例如,结构体或类的情况,如果没有比较运算符,max函数无法直接使用,必须提供比较方法。 还要考虑代码示例的清晰度,是否需要多个例子来覆盖不同情况。比如两个值比较、多个值比较、自定义比较函数,以及不同类型的参数,比如整数、浮点数或字符串。 最后,总结使用时的注意事项,比如参数类型必须一致,比较函数的要求,以及处理可能的异常情况,比如空列表。需要确保用户理解这些要点,避免常见错误。</think>以下是关于C++ STL中 `std::max` 函数的详细用法说明: --- ### **核心功能** `std::max` 用于比较**多个值**并返回最大值,适用于**直接比较值**(而非容器元素)。与 `std::max_element`(用于容器遍历)不同,它直接操作输入参数。 --- ### **基本用法** #### **1. 比较两个值** ```cpp #include <algorithm> // 必须包含头文件 int a = 5, b = 10; int max_value = std::max(a, b); // 返回10 ``` #### **2. 比较多个值(需C++11及以上)** 使用 `std::initializer_list`: ```cpp // 直接传入多个值 int max_val = std::max({3, 1, 4, 1, 5, 9}); // 返回9 // 或用变量组合 int x = 7, y = 2, z = 5; int max_val = std::max({x, y, z}); // 返回7 ``` --- ### **进阶用法** #### **1. 自定义比较函数** 通过第三个参数指定比较规则(例如比较字符串长度): ```cpp #include <string> #include <algorithm> // 自定义比较函数 bool compareLength(const std::string& a, const std::string& b) { return a.length() < b.length(); // 按长度比较 } int main() { std::string s1 = "apple", s2 = "banana"; auto longest = std::max(s1, s2, compareLength); // 返回"banana" return 0; } ``` #### **2. 比较不同类型(需隐式转换支持)** ```cpp double d = 3.14; int i = 5; auto max_val = std::max(d, i); // 返回5.0(int隐式转double) ``` --- ### **注意事项** 1. **参数要求**: - 参数类型必须一致,或支持隐式转换 - 自定义比较函数需返回 `bool`,且满足严格弱序关系 2. **空列表问题**: ```cpp std::max({}); // 编译通过,但行为未定义(不可用) ``` 使用 `initializer_list` 时必须确保列表非空 3. **自定义类型**: 若比较自定义类对象,需满足以下条件之一: - 类重载了 `operator<` - 提供自定义比较函数(如上述字符串长度示例) --- ### **代码示例汇总** ```cpp #include <iostream> #include <algorithm> #include <string> struct Point { int x, y; bool operator<(const Point& other) const { return x*x + y*y < other.x*other.x + other.y*other.y; // 按到原点距离比较 } }; int main() { // 示例1:基本数值比较 std::cout << std::max(3, 5) << "\n"; // 输出5 // 示例2:多值比较 std::cout << std::max({2.7, 3.14, 1.618}) << "\n"; // 输出3.14 // 示例3:自定义类型 Point p1{1, 2}, p2{3, 1}; std::cout << std::max(p1, p2).x << "," << std::max(p1, p2).y << "\n"; // 输出3,1 // 示例4:自定义比较函数 auto cmp = [](const auto& a, const auto& b) { return a%10 < b%10; }; // 按个位数比较 std::cout << std::max(19, 23, cmp); // 输出19(因为9 > 3) return 0; } ``` --- ### **总结** - **核心操作**:`std::max(a, b)` 或 `std::max({...})` - **适用场景**:直接比较值而非容器元素时 - **扩展性**:通过自定义比较函数支持复杂逻辑 - **安全建议**:避免对空列表使用 `std::max({})`
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值