C++及C中的 string char指针及char数组
2016-08-08 23:14
363 查看
转自: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类,定义在头文 件。注意
void main()
{
string s(“hehe”);
cout<
void main()
{
char chs[] = “hehe”;
string s(chs);
cout<
void main()
{
char chs[] = “hehe”;
string s(chs,1,3); //指定从chs的索引1开始,最后复制3个字节
cout<
void main()
{
string s1(“hehe”);
string s2(s1);
cout<
void main()
{
string s1(“hehe”,2,3);
string s2(s1);
cout<
void main()
{
char chs[] = “hehe”;
string s(chs,3); //将chs前3个字符作为初值构造
cout<
void main()
{
string s(10,’k’); //分配10个字符,初值都是’k’
cout<
void main()
{
string s(10,’k’); //分配10个字符,初值都是’k’
cout<
void main()
{
string s1 = “hehe”;
string s2 = “gagaga”;
cout<<”s1 : “<
void main()
{
string s = “hehe”;
s += “gaga”;
cout<
void main()
{
string s = “hehe”;
s.insert(0,”头部”); //在头部插入
s.insert(s.size(),”尾部”); //在尾部插入
s.insert(s.size()/2,”中间”);//在中间插入
cout<
void main()
{
string s = “abcdefg”;
s.erase(0,1); //从索引0到索引1,即删除掉了’a’
cout<
void main()
{
string s = “abcdefg”;
cout<
void main()
{
string s = “abcdefg”;
s.replace(2,3,”!!!!!”);//从索引2开始3个字节的字符全替换成”!!!!!”
cout<
void main()
{
string s1 = “abcdefg”;
string s2 = “abcdefg”;
if (s1==s2)cout<<”s1 == s2”<
void main()
{
string s = “abcdefg”;
cout<
void main()
{
string s = “abcdefg”;
cout<
void main()
{
string s ;
if (s.empty())
cout<<”s 为空.”<
void main()
{
string s = “abcdefg1111”;
}
21)
void main()
{
string s = “abcdefg1111”;
}
22)
// substr() 返回某个子字符串
void main()
{
string s = “abcdefg1111”;
}
23)
// find 查找函数
void main()
{
string s = “abcdefg1111”;
string pattern = “fg”;
string::size_type pos;
pos = s.find(pattern,0); //从索引0开始,查找符合字符串”f”的头索引
cout<
void main()
{
string s = “abcdefg1111”;
for(string::iterator iter = s.begin(); iter!=s.end(); iter++)
{
cout<<*iter<
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<
相关文章推荐
- C++及C中的 string char指针及char数组
- C++及C中的 string char指针及char数组
- 关于LinuxC/C++中字符指针char * str 与字符数组 char str []的一点小区别
- 关于LinuxC/C++中字符指针char * str 与字符数组 char str []的一点小区别
- 数据手动输入c++ 结构体练习 结构体重的char数组指针
- 在C++中如何将ActiveX控件所使用的BSTR数据转换成CHAR字符数组?
- C/C++数组名与指针区别
- 挑战30天C++入门极限-C/C++中利用数组名/指针进行排序实例
- C/C++中数组和指针类型的关系
- C/C++数组名与指针区别(转)
- 对C/C++函数中指针与数组的研究.
- 指针, 指针的指针, 数组, 指针数组, 数组指针, 指针函数, 函数指针 C/C++中函数指针的含义
- C/C++中字符指针数组及指向指针的指针的含义
- c/c++中的字符指针数组,指向指针的指针的含义
- C++复习 04 数组和指针
- C/C++数组名与指针区别深入探索
- C/C++中数组和指针的区别
- C/C++数组名与指针区别深层探索
- [转]c/c++中的字符指针数组,指向指针的指针的含义
- C/C++数组名与指针区别