The Assign method is understood to be to empty the original string first and then assign a new value to replace it.
Returns a reference of type string. Its common overloads are also available in the following ways:
A. string& assign (const string& str);
Replace Str with the contents of the original string
Example:
String testassign = "Hello World";
Testassign.assign ("Go home");
cout<<testassign<<endl;
Print results for Go home
B. string& assign (const string& STR, size_t pos, size_t N);
Assigns the contents of Str to the original string as a new content of the original string from the N characters from position pos
String testassign = "Hello World";
Testassign.assign ("Come on!", 5, 2);
cout<<testassign<<endl;
Print result is on
C. string& assign (const char* s, size_t N);
Replaces the first n characters of a character array or string with the contents of the original string
Example:
String testassign = "Hello World";
Testassign.assign ("Go Back to China", 7);
cout<<testassign<<endl;
Print results for Go back
D. string& assign (const char* s);
Replace the string or character array with the original as a new content
Example:
String testassign = "Hello World";
Char ch[20] = "Go back to Shanghai";
Testassign.assign (CH);
cout<<testassign<<endl;
Print results for go back to Shanghai
E. string& assign (size_t N, char c);
Replace the original string with n characters c
Example:
String testassign = "Hello World";
char ch = '? ';
Testassign.assign (5, CH);
cout<<testassign<<endl;
Print the result as?????
F. Template <class inputiterator> string& assign (inputiterator first, Inputiterator last);
Need include <iterator>
Example:
String testassign = "Hello World";
Testassign.assign (istream_iterator<char> (CIN), istream_iterator<char> ());
Input ABCDE
cout<<testassign<<endl;
Print result is ABCDE
---------------------------------------------------------------------------------------
string& Assign (const string& str);string& assign (const string& STR, size_t pos, size_t N); string& ; Assign (const char* s, size_t N);string& assign (const char* s);string& assign (size_t N, char c); Template & Lt;class inputiterator> string& Assign (inputiterator first, Inputiterator last);
Assign content to String
Assigns new content to the string replacing it current content.
The arguments passed to the function determine the new content:
-
-
string& Assign (const string& str);
-
-
sets a copy of the
str as the new content.
-
-
string& Assign (const string& STR, size_t pos, size_t N);
-
-
sets a copy of a substring of
str as the new content. The substring is the portion of
str . Begins at the character position
Pos and takes
n characters (it takes less than
n if the end of
str is reached before).
-
-
string& Assign (const char * s, size_t N);
-
-
sets as the new content a copy of the string formed by the first
n characters of the array pointed by C9>s.
-
-
string& Assign (const char * s);
-
-
sets a copy of the string formed by the null-terminated character sequence (C string) pointed by
s as The new content. The length of the character sequence is determined by the first ocurrence of a null character (as determined by Traits.len Gth (s)).
-
-
string& Assign (size_t N, char c);
-
-
sets a string formed by a repetition of character
C,
n times, as the new content.
-
-
Template<class inputiterator> string& Assign (inputiterator first, Inputiterator last);
-
-
If
inputiterator is an integral type, behaves as the previous member function version, effectively settin G as the new content a string formed by the repetition
first times of the character equivalent to
last.
The content is set to the values of the elements that go from element referred to by iterator
Fir St -to-the element right before the one referred-
to-iterator last.
Parameters
-
-
Str
-
-
another object of class string whose content is entirely or partially copied as the new content for the string.
-
-
Pos
-
-
starting position of the substring of the string Object
str that forms the new content. Notice that the first position have a value of 0, not 1.
If the position passed is past the end of
str, an out_of_range exception is thrown.
-
-
N
-
Number of
-
characters to use for the content (i.e., its length).
-
-
S
-
-
Array with a sequence of characters.
In the third member function version, the length was determined by parameter
N, even including null characters I n the content.
By contrast, in the fourth member version,
S was a null-terminated character, and therefore its length is determ Ined only by the first occurrence of a null character.
-
-
C
-
-
Character value to is repeated
n times to form the new content.
-
-
Start
-
-
If along with
last, both was integers, it's equivalent to parameter
n, otherwise it's an ITE Rator referring to the beginning of a sequence of characters.
-
-
Last
-
-
If along with
start, both is integers, it's equivalent to parameter
C, otherwise it's an IT Erator referring to the past-the-end element of a sequence of characters.
Return Value
*this
Example
1234567891011121314151617181920212223242526272829303132333435
|
//String::assign#include <iostream>#include <string>using namespaceStdintMain () {string str; String base="The quick brown fox jumps over a lazy dog.";//used in the same order as described above:Str.assign (base); cout << str << Endl; Str.assign (base,10,9); cout << str << Endl;//"Brown Fox"Str.assign ("pangrams are cool", 7); cout << str << Endl;//"Pangram"Str.assign ("c-string"); cout << str << Endl;//"c-string"Str.assign (10,‘*‘); cout << str << Endl;// "**********"str.assign<int> (10,0x2d); cout << str << Endl;// "----------"Str.assign (Base.begin () +16,base.end ()-12); cout << str << Endl;//"Fox jumps over" return0;}
|
C + + String detailed assign