成员函数Assign()用于分配,它为字符串分配一个新值,替换其当前内容。
语法1:分配字符串str的值。
string& string::assign (const string& str)
str : is the string to be assigned.
Returns : *this
CPP
// CPP code for assign (const string& str)
#include
#include
using namespace std;
// Function to demonstrate assign
void assignDemo(string str1, string str2)
{
// Assigns str2 to str1
str1.assign(str2);
cout << "After assign() : ";
cout << str1;
}
// Driver code
int main()
{
string str1("Hello World!");
string str2("GeeksforGeeks");
cout << "Original String : " << str1 << endl;
assignDemo(str1, str2);
return 0;
}
CPP
// CPP code to illustrate
// assign(const string& str, size_type
// str_idx, size_type str_num)
#include
#include
using namespace std;
// Function to demonstrate assign
void assignDemo(string str1, string str2)
{
// Assigns 8 characters from
// 5th index of str2 to str1
str1.assign(str2, 5, 8);
cout << "After assign() : ";
cout << str1;
}
// Driver code
int main()
{
string str1("Hello World!");
string str2("GeeksforGeeks");
cout << "Original String : " << str1 << endl;
assignDemo(str1, str2);
return 0;
}
CPP
// CPP code for assign (const char* cstr)
#include
#include
using namespace std;
// Function to demonstrate assign
void assignDemo(string str)
{
// Assigns GeeksforGeeks to str
str.assign("GeeksforGeeks");
cout << "After assign() : ";
cout << str;
}
// Driver code
int main()
{
string str("Hello World!");
cout << "Original String : " << str << endl;
assignDemo(str);
return 0;
}
CPP
// CPP code to illustrate
// string& string::assign
// (const char* chars, size_type chars_len)
#include
#include
using namespace std;
// Function to demonstrate assign
void assignDemo(string str)
{
// Assigns first 5 characters of
// GeeksforGeeks to str
str.assign("GeeksforGeeks", 5);
cout << "After assign() : ";
cout << str;
}
// Driver code
int main()
{
string str("Hello World!");
cout << "Original String : " << str << endl;
assignDemo(str);
return 0;
}
CPP
// CPP code for string&
// string::assign (size_type num, char c)
#include
#include
using namespace std;
// Function to demonstrate assign
void assignDemo(string str)
{
// Assigns 10 occurrences of 'x'
// to str
str.assign(10, 'x');
cout << "After assign() : ";
cout << str;
}
// Driver code
int main()
{
string str("#########");
cout << "Original String : " << str << endl;
assignDemo(str);
return 0;
}
CPP
// CPP code for string&
// string::assign (size_type num, char c)
#include
#include
using namespace std;
// Function to demonstrate assign
void assignDemo(string str)
{
string str1;
// Assigns all characters between
// str.begin()+6 and str.end()-0 to str1
str1.assign(str.begin()+6, str.end()-0);
cout << "After assign() : ";
cout << str1;
}
// Driver code
int main()
{
string str("Hello World!");
cout << "Original String : " << str << endl;
assignDemo(str);
return 0;
}
输出:
Original String : Hello World!
After assign() : GeeksforGeeks
语法2:最多分配以索引str_idx开头的str的str_num个字符。如果str_idx> str,则抛出_range。尺寸()。
string& string::assign (const string& str, size_type str_idx, size_type str_num)
str : is the string to be assigned.
str_idx : is the index number in str.
str_num : is the number of characters picked
from str_idx to assign
Return : *this
CPP
// CPP code to illustrate
// assign(const string& str, size_type
// str_idx, size_type str_num)
#include
#include
using namespace std;
// Function to demonstrate assign
void assignDemo(string str1, string str2)
{
// Assigns 8 characters from
// 5th index of str2 to str1
str1.assign(str2, 5, 8);
cout << "After assign() : ";
cout << str1;
}
// Driver code
int main()
{
string str1("Hello World!");
string str2("GeeksforGeeks");
cout << "Original String : " << str1 << endl;
assignDemo(str1, str2);
return 0;
}
输出:
Original String : Hello World!
After assign() : forGeeks
语法3:指定C-串CSTR的字符。如果结果大小超过最大字符数,则抛出length_error。
string & string::assign (const char* cstr)
Assigns all characters of cstr up to but not including '\0'.
Returns : *this.
Note : that cstr may not be a null pointer (NULL).
CPP
// CPP code for assign (const char* cstr)
#include
#include
using namespace std;
// Function to demonstrate assign
void assignDemo(string str)
{
// Assigns GeeksforGeeks to str
str.assign("GeeksforGeeks");
cout << "After assign() : ";
cout << str;
}
// Driver code
int main()
{
string str("Hello World!");
cout << "Original String : " << str << endl;
assignDemo(str);
return 0;
}
输出:
Original String : Hello World!
After assign() : GeeksforGeeks
语法4:指定chars_len字符数组字符的字符。如果结果大小超过最大字符数,则抛出length_error。
string& string::assign (const char* chars, size_type chars_len)
*chars : is the pointer to the array to be assigned.
chars_len : is the number of characters to be assigned from
character array.
Note : that chars must have at least chars_len characters.
Returns : *this.
CPP
// CPP code to illustrate
// string& string::assign
// (const char* chars, size_type chars_len)
#include
#include
using namespace std;
// Function to demonstrate assign
void assignDemo(string str)
{
// Assigns first 5 characters of
// GeeksforGeeks to str
str.assign("GeeksforGeeks", 5);
cout << "After assign() : ";
cout << str;
}
// Driver code
int main()
{
string str("Hello World!");
cout << "Original String : " << str << endl;
assignDemo(str);
return 0;
}
输出:
Original String : Hello World!
After assign() : Geeks
语法5:指定字符c的出现次数。如果num等于字符串:: npos则抛出length_error
string & string::assign (size_type num, char c)
num : is the number of occurrences to be assigned.
c : is the character which is to be assigned repeatedly.
Throws length_error if the resulting size exceeds the maximum number(max_size) of characters.
Returns : *this.
CPP
// CPP code for string&
// string::assign (size_type num, char c)
#include
#include
using namespace std;
// Function to demonstrate assign
void assignDemo(string str)
{
// Assigns 10 occurrences of 'x'
// to str
str.assign(10, 'x');
cout << "After assign() : ";
cout << str;
}
// Driver code
int main()
{
string str("#########");
cout << "Original String : " << str << endl;
assignDemo(str);
return 0;
}
输出:
Original String : #########
After assign() : xxxxxxxxxx
语法6:分配范围[beg,end)的所有字符。如果范围超出字符串的实际内容,则会引发length_error 。
template
string& assign (InputIterator first, InputIterator last)
first, last : Input iterators to the initial and final positions
in a sequence.
Returns : *this.
CPP
// CPP code for string&
// string::assign (size_type num, char c)
#include
#include
using namespace std;
// Function to demonstrate assign
void assignDemo(string str)
{
string str1;
// Assigns all characters between
// str.begin()+6 and str.end()-0 to str1
str1.assign(str.begin()+6, str.end()-0);
cout << "After assign() : ";
cout << str1;
}
// Driver code
int main()
{
string str("Hello World!");
cout << "Original String : " << str << endl;
assignDemo(str);
return 0;
}
输出:
Original String : Hello World!
After assign() : World!
要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程” 。