How to use the assign function In C++
It accepts two arguments, starting pointer and ending pointer, and converts the char pointer array into a string.
C++
// C++ Program to demonstrate the conversion // of char* to string using assign function #include <iostream> using namespace std; int main() { const char * ch = "Welcome to w3wiki" ; string str; // 24 is the size of ch str.assign(ch, ch + 24); cout << str; return 0; } |
Output
Welcome to w3wiki
Time complexity: O(N), as time complexity of assign() is O(N) where N is the size of the new string
Auxiliary space: O(1).
Convert char* to string in C++
There are three ways to convert char* into string in C++.
- Using the “=” operator
- Using the string constructor
- Using the assign function