c++string容器(assign的用法)
·
c++string容器
引言:以前一直不知道string具体是个什么,今天才知道string和vector一样,都属于c++容器,要使用string容器,需要包含#include< string >头文件
c++的常见容器类型
1.string容器
2.vector容器
3.deque容器
4.stack容器
5.queue容器
6.list容器
c++string容器代码示例(assign的用法)
以常见的assign方法为例(assign的用法)
// STL中容器嵌套容器
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>//要使用for_each必须包含此头文件
using namespace std;
int main()
{
//assign 分派,布置(工作、任务);分配(某物);指派,派遣;确定(价值、功能、时间、地点);转让(财产、权利)
string str1;
str1.assign("hello world",5);
cout<<str1<<endl;
string str2;
str2.assign(str1);
cout<<str2<<endl;
string str3;
str3.assign(10,'W');
cout<<str3<<endl;
//string& assign(const string &s,int start,int n);
//将s 从下标为start的字符开始复制 , 复制n个
string str4;
str4.assign("sdsadsadasd",4,6);
cout<<str4<<endl;
return 0;
}

更多推荐
所有评论(0)