C++及C中的 string char指针及char数组

来源:互联网 发布:unity3d象棋素材 编辑:程序博客网 时间:2024/05/20 19:19

转自:http://www.cnblogs.com/ffhajbq/archive/2012/06/01/2529354.html

C++及C中的 string char指针及char数组

C++中string是标准库中一种容器,相当于保存元素类型为char的vector容器(自己理解),这个类提供了相当丰富的函数来完成对字符串操作,以及与C风格字符串之间转换,下面是对string一些总结<引用>

一,C语言的字符串

在C语言里,对字符串的处理一项都是一件比较痛苦的事情,因为通常在实现字符串的操作的时候都会用到最不容易驾驭的类型——指针。

比如下面这个例子:

//example 1:

char str[12] = “Hello”;

char *p = str;

*p = ‘h’; //改变第一个字母

//example 2:

char *ptr = “Hello”;

*ptr = ‘h’; //错误

第一个字符串时用数组开辟的,它是可以改变的变量。而第二个字符串则是一个常量,也就是字面值。ptr只是指向它的指针而已,而不能改变指向的内容。

看两者的汇编即可明了:

char p[] = “Hello”;
004114B8 mov eax,dword ptr [string “Hello” (4166FCh)]
004114BD mov dword ptr [ebp-10h],eax
004114C0 mov cx,word ptr ds:[416700h]
004114C7 mov word ptr [ebp-0Ch],cx

char *ptr = “Hello”;
004114CB mov dword ptr [ebp-1Ch],offset string “Hello” (4166FCh)

可见用数组和用指针是完全不相同的。

要想通过指针来改变常量是错误,正确的写法应该是用const指针。

const char *ptr = “Hello”;

二,初识string类

正是因为C风格字符串(以空字符结尾的字符数组)太过复杂难于掌握,不适合大程序的开发,所以C++标准库定义了一种string类,定义在头文 件。注意

include

include

using namespace std;

void main()
{
string s(“hehe”);
cout<

include

include

using namespace std;

void main()
{
char chs[] = “hehe”;
string s(chs);
cout<

include

include

using namespace std;

void main()
{
char chs[] = “hehe”;
string s(chs,1,3); //指定从chs的索引1开始,最后复制3个字节
cout<

include

include

using namespace std;

void main()
{
string s1(“hehe”);
string s2(s1);
cout<

include

include

using namespace std;

void main()
{
string s1(“hehe”,2,3);
string s2(s1);
cout<

include

include

using namespace std;

void main()
{
char chs[] = “hehe”;
string s(chs,3); //将chs前3个字符作为初值构造
cout<

include

include

using namespace std;

void main()
{
string s(10,’k’); //分配10个字符,初值都是’k’
cout<

include

include

using namespace std;

void main()
{
string s(10,’k’); //分配10个字符,初值都是’k’
cout<

include

include

using namespace std;

void main()
{
string s1 = “hehe”;
string s2 = “gagaga”;
cout<<”s1 : “<

include

include

using namespace std;

void main()
{
string s = “hehe”;
s += “gaga”;
cout<

include

include

using namespace std;

void main()
{
string s = “hehe”;
s.insert(0,”头部”); //在头部插入
s.insert(s.size(),”尾部”); //在尾部插入
s.insert(s.size()/2,”中间”);//在中间插入
cout<

include

include

using namespace std;

void main()
{
string s = “abcdefg”;
s.erase(0,1); //从索引0到索引1,即删除掉了’a’
cout<

include

include

using namespace std;

void main()
{
string s = “abcdefg”;
cout<

include

include

using namespace std;

void main()
{
string s = “abcdefg”;
s.replace(2,3,”!!!!!”);//从索引2开始3个字节的字符全替换成”!!!!!”
cout<

include

include

using namespace std;

void main()
{
string s1 = “abcdefg”;
string s2 = “abcdefg”;
if (s1==s2)cout<<”s1 == s2”<

include

include

using namespace std;

void main()
{
string s = “abcdefg”;
cout<

include

include

using namespace std;

void main()
{
string s = “abcdefg”;
cout<

include

include

using namespace std;

void main()
{
string s ;
if (s.empty())
cout<<”s 为空.”<

include

include

using namespace std;

void main()
{
string s = “abcdefg1111”;

cout<<"use []:"<<endl;for(int i=0; i<s.length(); i++){    cout<<s[i]<<endl;}cout<<endl;cout<<"use at():"<<endl;for(int i=0; i<s.length(); i++){    cout<<s.at(i)<<endl;}cout<<endl;cin.get();

}
21)

include

include

using namespace std;

void main()
{
string s = “abcdefg1111”;

const char * chs1 = s.c_str();const char * chs2 = s.data();cout<<"use at():"<<endl;int i;for(i=0; i<s.length(); i++){    cout<<"c_str() : "<<chs1[i]<<endl;    cout<<"data() : "<<chs2[i]<<endl;}cout<<"c_str() : "<<chs1<<endl;cout<<"data() : "<<chs2<<endl;cout<<endl;cin.get();

}
22)
// substr() 返回某个子字符串

include

include

using namespace std;

void main()
{
string s = “abcdefg1111”;

string str = s.substr(5,3);//从索引5开始3个字节cout<<str<<endl;cin.get();

}
23)
// find 查找函数

include

include

using namespace std;

void main()
{
string s = “abcdefg1111”;
string pattern = “fg”;
string::size_type pos;
pos = s.find(pattern,0); //从索引0开始,查找符合字符串”f”的头索引
cout<

include

include

using namespace std;

void main()
{
string s = “abcdefg1111”;
for(string::iterator iter = s.begin(); iter!=s.end(); iter++)
{
cout<<*iter<

0 0
原创粉丝点击