c++ std string reserve 测试
2014-04-10 11:52
369 查看
#include "windows.h" #include <tchar.h> #include <cstdio> #include <string> #include <limits> using namespace std; /* //z 2014-04-10 11:54:59 BG57IV3@XCL T1688293017.K.F1288753923[T13,L274,R5,V285] 输出结果,观察数据可见其策略。 可见一般情况下实际 reserve 的 capacity 比 申请的要大,当大于某个数值之后,变成逐个增加了,而且由于申请的内存巨大,非常消耗资源。 15 31 47 70 105 157 235 352 528 792 1188 1782 2673 4009 6013 9019 13528 20292 30438 45657 68485 102727 154090 231135 346702 520053 780079 1170118 1755177 2632765 3949147 5923720 8885580 13328370 19992555 29988832 44983248 67474872 101212308 151818462 227727693 341591539 512387308 512387309 768580963 768580964 768580965 768580966 768580967 768580968 768580969 768580970 */ void Test_String_Reserve() { string str; TCHAR buff[16] = {'\0'}; size_t nCapacity = str.capacity(); int intMax = (std::numeric_limits<int>::max)(); for (int i = 1 ; i < intMax ;++i) { str.reserve(i); if(nCapacity!=str.capacity()) { _stprintf(buff,_T("%d"),nCapacity); OutputDebugString(buff); nCapacity = str.capacity(); } } _stprintf(buff,_T("%d"),nCapacity); OutputDebugString(buff); } int _tmain(int argc, _TCHAR* argv[]) { Test_String_Reserve(); return 0; }
//z 2014-04-10 11:54:59 BG57IV3@XCL T1688293017.K.F1288753923[T13,L274,R5,V285]
/* 输出: 从大往小reserve,可见一般一直保留不变,直到n小于等于15。由于采取了优化的措施(通常保留 capacity 不变),这部分运行较快。 //z 2014-04-10 12:05:54 BG57IV3@XCL T3063169329.K.F1288753923[T14,L328,R5,V286] 768580975 15 */ void Test_String_Reserve_2() { string str; const int nReserve = 768580964; str.reserve(nReserve); TCHAR buff[16] = {'\0'}; size_t nCapacity = str.capacity(); //int intMax = (std::numeric_limits<int>::max)(); for (int i = nReserve ; i > 0 ;i-=15) { str.reserve(i); if(nCapacity!=str.capacity()) { _stprintf(buff,_T("%d"),nCapacity); OutputDebugString(buff); nCapacity = str.capacity(); } } _stprintf(buff,_T("%d"),nCapacity); OutputDebugString(buff); }
//z 2014-04-10 13:47:04 BG57IV3@XCL T2734643987.K.F1288753923[T17,L426,R8,V350]
/* 输出: win7 x64 (visual studio 2005 | win32 build) 4294967294 //z maxsize 4294967295 //z unsigned int::max value */ void GetStringMaxSize() { TCHAR buff[16] = {'\0'}; string str; size_t nMaxSize = str.max_size(); _stprintf(buff,_T("%u"),nMaxSize); OutputDebugString(buff); unsigned int nMax = (std::numeric_limits<unsigned int>::max)(); _stprintf(buff,_T("%u"),nMax); OutputDebugString(buff); }
相关文章推荐
- c++代码测试-std::string元素
- C++ #include<string> 和 using std::string
- Android NDK编译C++ 'stoi' and 'to_string' is not a member of 'std'
- 实战c++中的string系列--std:vector<char> 和std:string相互转换(vector to stringstream)
- Dev-C++ “to_string is not a member of std” error- 已解决
- C++学习笔记-----std::string的=,+,+=对int,char类型操作数的支持
- c++中std::string使用的一点新体会
- 将MFC的CString 类型转化为C++标准数据类型std::string
- C++: std::string 与 Unicode 如何结合?
- 2015-1-9_C++中奇怪的std:string
- C++ STL : std::string
- std::string转化大小写(C++)
- C++——std::String
- 实战c++中的string系列--std:vector<char> 和std:string相互转换(vector to stringstream)
- 几种C++ std::string和std::wstring相互转换的转换方法
- 实战c++中的string系列--std::string与MFC中CString的转换
- C++中实现从std::string类型到bool型的转换
- 几种C++ std::string和std::wstring相互转换的转换方法
- VS2005:C++ std::string, std::wstring转换方法
- C++中实现从std::string类型到bool型的转换