當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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++。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。