اگر کل رشته رو می خواهید کپی کنید به راحتی میتونین از = استفاده کنین
string a="abcde";
string b=a;
اگر قسمت خاصی از رشته رو می خواهید توی یک رشته دیگه کپی کنید از std::copy موجود در هدر algorithm استفاده کنین . یا از copy داخل کلاس string .
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
string a="abcde";
string b="1122";
std::copy(a.begin()+1,a.end()-2,b.begin()+2);
cout<<b;// khorooji 11bc
}
اگر رشته رو میخواهید بین یک رشته دیگه قرار بدین از insert استفاده کنین .
کد از string::insert
#include <iostream>
#include <string>
int main ()
{
std::string str="to be question";
std::string str2="the ";
std::string str3="or not to be";
std::string::iterator it;
// used in the same order as described above:
str.insert(6,str2); // to be (the )question
str.insert(6,str3,3,4); // to be (not )the question
str.insert(10,"that is cool",8); // to be not (that is )the question
str.insert(10,"to be "); // to be not (to be )that is the question
str.insert(15,1,':'); // to be not to be(:) that is the question
it = str.insert(str.begin()+5,','); // to be(,) not to be: that is the question
str.insert (str.end(),3,'.'); // to be, not to be: that is the question(...)
str.insert (it+2,str3.begin(),str3.begin()+3); // (or )
std::cout << str << '\n';
return 0;
}