string类的七种构造函数
来源:互联网 发布:java中pack 编辑:程序博客网 时间:2024/06/05 18:17
string类也是常用的东西,很重要!
1. 七种构造函数
string (); //最简单的一种,无参构造函数
string (const char * s);
string (size_type n,char c);
string (const char * s,size_type n);
string (const string & str,size_tpye n=npos); //带默认值的
template<class Iter> string(Iter begin,Iter end); //使用迭代器的
string::npos (通常为最大unsigned int值,比最大索引大1) //常与find函数连用,判断find的结果
2、例子
//string_example.cpp
#include <iostream>
#include <string>
using namespace std;
int main()
{
string one("string construct !"); //string (const char * s);
cout << one << endl;
string two(20, '$'); //string (size_type n,char c);
cout << two << endl;
string three(one); //copy construct function
cout << three << endl;
one += " RYP_S!"; //append
cout << one << endl;
two = "hello world ! ";
three[0] = 'P'; //substitution
string four;
four = two + three; //join
cout << four << endl;
char alls[] = "stay hungry,stay foolish!";
string five(alls,20); // string (const char * s,size_type n);
cout << five << "!\n";
string six(alls+6, alls + 10); // template<class Iter> string(Iter begin,Iter end);
cout << six << ", ";
string seven(&five[6], &five[10]); // template<class Iter> string(Iter begin,Iter end);
cout << seven << "...\n"<<endl;
return 0;
}
运行结果如下:
- string类的七种构造函数
- string类的六种构造函数
- string类的构造函数
- 类String的构造函数
- string类的构造函数
- string类的构造函数
- string 的构造函数
- String 类构造函数
- java中String类的构造函数
- java中String类的构造函数
- string类的构造函数实现
- c++ string类的构造函数
- C++ string类的构造函数
- string类的八个构造函数用法
- java中String类的构造函数
- C++基础---string类的构造函数
- C++ string类的构造函数
- Java中String类的构造函数
- 桌面时钟问题诊断
- MFC 点击按钮,弹出另一个对话框
- [MEF程序设计指南]定义可以组合的部件和契约
- Java常用事件
- 文件操作
- string类的七种构造函数
- 记下毕业季找房子相关事项
- ADODB.Command ASP环境下应用方法
- 2013Esri全球用户大会Q&A之3D新动向
- The Book of QT4 翻译:2.1.1-2.1.2 对话框和小部件
- c语言的数组、指针、函数传递释义
- andengine-4: TexturePack的学习
- this指针
- GCD笔记