Strings_assign_学习 简单

#include <iostream>
#include <string>
using namespace std;
void assign()
{
	string str="assign";
	char str_one='i';
	cout<<"size_type num, const char& val\n";
	//添加指定数量的字符到str中去
	str.assign(6,str_one);
	cout<<str<<endl;

	//添加字符串到str中去
	string str_two="cosnt string& str";
	str.assign(str_two);
	cout<<str<<endl;

	//添加字符指针到str中去
	char* str_three="const char* str";
	str.assign(str_three);
	cout<<str<<endl;

	//添加字符指针的指定数量到str中去
	str.assign(str_three,5);
	cout<<str<<endl;

	//添加字符串的指定字符长度到str中去
	string str_four="const string&, size_type index, size_type len";
	str.assign(str_four,0,str_four.length());
	cout<<str<<endl;

	char ch_one = 's';
	str.assign(10,ch_one);
	cout<<str<<endl;
};

int main()
{
     assign();
	 system("pause");
	 return 0;
}

  

原文地址:https://www.cnblogs.com/xiangxiaodong/p/2352491.html