C++之STL之string
2016-10-16 12:24
363 查看
/*C 语言中字符数组一般会采用char str[]来存放,但是显得会比较麻烦,C++在stl中加入
了string类型,对字符串常用的功能进行了封装,操作起来比较方便*/
输出结果如下:
hello world
通过迭代器进行访问
/*string的拼接*/
输出结果::
hello world
hello world
了string类型,对字符串常用的功能进行了封装,操作起来比较方便*/
#include<cstdio> #include<string> using namespace std; int main(){ string str = "hello world"; for (int i = 0; i< str.length(); i++){ printf("%c",str[i]); } return 0; }
输出结果如下:
hello world
通过迭代器进行访问
#include<cstdio> #include<string> using namespace std; int main(){ string str = "hello world"; /*通过迭代器进行访问*/ for (string::iterator it = str.begin(); it != str.end(); it ++){ printf("%c",*it); } return 0; }
/*string的拼接*/
#include<iostream> #include<string> using namespace std; int main(){ string str1 = "hello world", str2 = "form China voice", str3; str3 = str1 + str2; //将str1和str2拼接,直接赋值给str3 str1 += str2; // 将str2直接拼接到str1上 cout<<str3<<endl; cout<<str1<<endl; return 0; }
输出结果::
hello world
hello world
相关文章推荐
- C++ STL 帮助文档 -- string
- C++中string的用法STL
- c++ stl 使用汇总(string,vector,map,set)
- [转载] C++ STL string的Copy-On-Write技术
- linux c++ stl hash_map basic_string 使用
- Power up C++ with STL: Part II (string, set, map)
- C++STL之string上
- C++学习笔记(六):复杂数据类型(string、容器和STL)
- C++ STL string
- c++ stl string char* 向 string 转换的问题
- 【语法回顾】C++STL:string类型
- C++STL之string
- C++STLstring
- C++ String STL
- C++ STL学习笔记2--String
- string::npos 是什么 c++ /STL
- C++ Standard Stl -- SGI STL源码学习笔记(08) string
- C++ STL string 大小写转换时的 no matching function for call to ‘transform 错误
- C++ STL之string
- c++STL之string