转自:http://www.cppblog.com/MemoryGarden/archive/2009/12/28/104244.aspx
1 // string::assign
2 #include <iostream>
3 #include <string>
4 using namespace std;
5
6 int main ()
7 {
8 string str;
9 string base="The quick brown fox jumps over a lazy dog.";
10
11 // used in the same order as described above:
12
13 str.assign(base);
14 cout << str << endl;
15
16 str.assign(base,10,9);
17 cout << str << endl; // "brown fox"
18
19 str.assign("pangrams are cool",7);
20 cout << str << endl; // "pangram"
21
22 str.assign("c-string");
23 cout << str << endl; // "c-string"
24
25 str.assign(10,'*');
26 cout << str << endl; // "**********"
27
28 str.assign<int>(10,0x2D);
29 cout << str << endl; // "----------"
30
31 str.assign(base.begin()+16,base.end()-12);
32 cout << str << endl; // "fox jumps over"
33
34 return 0;
35 }