C++ 中的string的简单使用

//----------------------------------------------------

//AUTHOR: lanyang123456

//DATE: 2014-10-21

//---------------------------------------------------



/*
stringtest.cpp

$ g++ -o test stringtest.cpp

OS:64bit OS Ubuntu
*/

#include <string>
#include <iostream>

#include <string.h>
#include <stdio.h>

using namespace std;

int main()
{
	string teststring("OK OK OK OK OK OK ");
	char buffer[] = " hello  hello  hello  hello  hello  hello ";
	
	cout<<"Now the string:"<<teststring<<endl;
	printf("Now the string:%s\n",teststring.c_str());//返回一个以null终止的c字符串
	printf("sizeof string = %lu\n", sizeof(string));
	printf("string.capacity = %lu\n", teststring.capacity());//返回当前容量(即string中不必增加内存即可存放的元素个数)
	printf("string.max_size = %lu\n", teststring.max_size());//返回string对象中可存放的最大字符串的长度
	printf("string.size = %lu\n", teststring.size());//返回当前字符串的大小
	printf("string.length = %lu\n", teststring.length());//返回当前字符串的长度

	teststring.append("two");
	printf("after the first append\n");
	cout<<"Now the string"<<teststring<<endl;
	printf("sizeof teststring = %lu\n", sizeof(teststring));
	printf("string.capacity = %lu\n", teststring.capacity());
	printf("string.max_size = %lu\n", teststring.max_size());
	printf("string.size = %lu\n", teststring.size());
	printf("string.length = %lu\n", teststring.length());

	teststring.append(buffer, strlen(buffer));
	printf("after the second append\n");
	cout<<"Now the string"<<teststring<<endl;
	printf("sizeof teststring = %lu\n", sizeof(teststring));
	printf("string.capacity = %lu\n", teststring.capacity());
	printf("string.max_size = %lu\n", teststring.max_size());
	printf("string.size = %lu\n", teststring.size());
	printf("string.length = %lu\n", teststring.length());

	teststring.erase(0, teststring.length());
	printf("after erase\n");
	cout<<"Now the string"<<teststring<<endl;
	printf("sizeof teststring = %lu\n", sizeof(teststring));
	printf("string.capacity = %lu\n", teststring.capacity());
	printf("string.max_size = %lu\n", teststring.max_size());
	printf("string.size = %lu\n", teststring.size());
	printf("string.length = %lu\n", teststring.length());


	return 0;
}

/*
$ ./test 
Now the string:OK OK OK OK OK OK 
Now the string:OK OK OK OK OK OK 
sizeof string = 8
string.capacity = 18
string.max_size = 4611686018427387897
string.size = 18
string.length = 18
after the first append
Now the stringOK OK OK OK OK OK two
sizeof teststring = 8
string.capacity = 36
string.max_size = 4611686018427387897
string.size = 21
string.length = 21
after the second append
Now the stringOK OK OK OK OK OK two hello  hello  hello  hello  hello  hello 
sizeof teststring = 8
string.capacity = 72
string.max_size = 4611686018427387897
string.size = 63
string.length = 63
after erase
Now the string
sizeof teststring = 8
string.capacity = 72
string.max_size = 4611686018427387897
string.size = 0
string.length = 0

*/






string 在存储字符串时分配的内存一般比当前实际的内存大一些,capacity()返回值就可以看出,这主要是为了避免频繁的内存操作。并且将string清除后,capacity大小变化,说明内存空间还在占用中。





参考


http://www.cnblogs.com/xFreedom/archive/2011/05/16/2048037.html

你可能感兴趣的:(C++ 中的string的简单使用)