C++基础---string类的data/c_str/copy
2015-09-03 18:09
806 查看
1.string类的data/c_str/copy
1.1 std::string::data
原型: const char *data()const;功能: string型字符串转化为char型字符串。
说明:将自身转化为一个以空终止符结束(即带”\0“结尾)的char型字符串。
返回值:一个指向字符串数组的指针,字符串数组指针。
代码示例:
(1)示例代码一: #include <iostream> #include <string> using namespace std; int main () { string str = "Test string"; char *cstr = "Test string"; if (str.length() == strlen(cstr)) { cout<<"str and cstr have the same length."<<endl; if (memcmp(cstr, str.data(), str.length()) == 0) { cout<<"str and cstr have the same content."<<endl; } } system("pause"); return 0; } =>str and cstr have the same length. str and cstr have the same content.
(2)示例代码二: #include <iostream> #include <string> using namespace std; int main () { string str("Please split this sentence into tokens"); const char *split = " "; char *cstr = new char [str.length()+1]; strcpy_s(cstr, str.size() + 1, str.data()); //cstr now contains a c-string copy of str char *p = 0; char *pNext = 0; p = strtok_s(cstr, split, &pNext); while(p != 0) { cout<<p<<endl; p = strtok_s(NULL, split, &pNext); } delete[] cstr; system("pause"); return 0; } =>Please split this sentence into tokens
1.2 std::string::c_str
原型:const char *c_str()const;功能: string型字符串转化为char型字符串。
说明:将自身转化为一个以空终止符结束(即带”\0“结尾)的char型字符串。
返回值:一个指向字符串数组的指针,字符串数组指针。
代码示例:
(1)示例代码一: #include <iostream> #include <string> using namespace std; int main () { string str = "Test string"; char *cstr = "Test string"; if (str.length() == strlen(cstr)) { cout<<"str and cstr have the same length."<<endl; if (memcmp(cstr, str.c_str(), str.length()) == 0) { cout<<"str and cstr have the same content"<<endl; } } system("pause"); return 0; } =>str and cstr have the same length. str and cstr have the same content.
(2)示例代码二: #include <iostream> #include <string> using namespace std; int main () { string str("Please split this sentence into tokens"); const char *split = " "; char *cstr = new char [str.length()+1]; strcpy_s(cstr, str.size() + 1, str.c_str()); //cstr now contains a c-string copy of str char *p = 0; char *pNext = 0; p = strtok_s(cstr, split, &pNext); while (p != 0) { cout<<p<<endl; p = strtok_s(NULL, split, &pNext); } delete[] cstr; system("pause"); return 0; } =>Please split this sentence into tokens
1.3 std::string::copy
原型:size_t copy (char* s, size_t len, size_t pos = 0) const;功能: string型字符串转化为char型字符串。
说明:从源字符串以下标为pos(默认为0)处开始拷贝n个字符到char型字符串s中。
返回值:实际拷贝的字符个数。
代码示例:
#pragma warning(disable:4996) //全部关掉 #include <iostream> #include <string> using namespace std; int main () { char buffer[20]; string str("Test string..."); size_t length = str.copy(buffer, 6, 5); buffer[length]='\0'; cout<<"buffer contains: "<<buffer<<endl; system("pause"); return 0; } =>buffer contains: string
参考文献:
[1] 网络资源: http://www.cplusplus.com/reference/string/string/data/
http://www.cplusplus.com/reference/string/string/c_str/
http://www.cplusplus.com/reference/string/string/copy/
相关文章推荐
- C++基础---string类的构造函数
- 来看看C语言的-1>1并且小有1的问题
- C++基础---字符串(string型字符串)
- c语言实现顺序二叉树的非递归的三种遍历算法
- 【C语言】结构体
- C++内存管理及指针相关
- C++入门经典 笔记 (第十章)创建指针
- Qt C++中的关键字explicit
- 零基础学C语言 笔记八 二维数组
- C语言中文件包含#include使用尖括号和双引号的区别
- C++入门经典 笔记 (第九章)高级类
- c++中输入和输入语句
- c++ primer读书笔记之c++11(三)
- C语言实验题——三个数排序
- VS2010中“工具>选项中的VC++目录编辑功能已被否决”解决方法
- 关于C++中的友元函数的总结
- 关于C语言中的cons的使用。
- 零基础学C语言 笔记七 一维数组
- C++入门经典 笔记 (第八章)创建基本类
- C语言中怎么将文件里的数据创建到(读到)链表中?