ST::String:C++中的赋值()

成员函数 分配() 用于赋值,它为字符串赋值,替换其当前内容。 语法1: 指定string str的值。

null
string& string::assign (const string& str)str :  is the string to be assigned.Returns : *this

CPP

// CPP code for assign (const string& str)
#include <iostream>
#include <string>
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;
}


输出:

Original String : Hello World!After assign() : GeeksforGeeks

语法2: 最多分配以索引str_idx开头的str_num个字符。如果str_idx>str.size(),则抛出_的范围。

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 assignReturn : *this

CPP

// CPP code to illustrate
// assign(const string& str, size_type
// str_idx, size_type str_num)
#include <iostream>
#include <string>
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的字符。如果结果大小超过最大字符数,则抛出长度_错误。

string & string::assign (const char* cstr)Assigns all characters of cstr up to but not including ' '.Returns : *this.Note : that cstr may not be a null pointer (NULL).

CPP

// CPP code for assign (const char* cstr)
#include <iostream>
#include <string>
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: 指定字符数组字符中的字符。如果结果大小超过最大字符数,则抛出长度_错误。

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 <iostream>
#include <string>
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。如果num等于string::npos,则抛出长度_错误

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 <iostream>
#include <string>
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]范围内的所有字符。它抛出 长度误差 如果范围超出字符串的实际内容。

template <class InputIterator>   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 <iostream>
#include <string>
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!

本文由 萨希·提瓦里 .如果你喜欢极客(我们知道你喜欢!)如果你想投稿,你也可以用 贡献极客。组织 或者把你的文章寄到contribute@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。 如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。

© 版权声明
THE END
喜欢就支持一下吧
点赞12 分享