当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


C++ std::string::assign()用法及代码示例


成员函数assign()用于分配,它为字符串分配一个新值,以替换其当前内容。语法1:分配字符串str的值。

string& string::assign (const string& str)

str: is the string to be assigned.
返回: *this
// 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的str_num个字符。如果str_idx> str,则抛出_range _range。 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 assign
返回: *this
// 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 13 charaacters from 
    // 5th index of str2 to str1 
    str1.assign(str2, 5, 13); 
    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-string cstr的字符。如果结果大小超过最大字符数,它将抛出length_error。

string & string::assign (const char* cstr)

Assigns all characters of cstr up to but not including '\0'.
返回: *this.
Note: that cstr may not be a null pointer (NULL).
// 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:分配字符数组chars的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.
返回: *this.
// 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

Synatx 5:分配字符c的出现次数。如果num等于string::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.
返回: *this.
// 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)的所有字符。如果范围超出了字符串的实际内容,它将引发length_error。

template <class InputIterator>
   string& assign (InputIterator first, InputIterator last)
first, last:Input iterators to the initial and final positions 
in a sequence.

返回: *this.
// 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!


相关用法


注:本文由纯净天空筛选整理自 std::string::assign() in C++。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。