Skip to content

Latest commit

 

History

History
82 lines (66 loc) · 2.46 KB

File metadata and controls

82 lines (66 loc) · 2.46 KB

String

Strings are defined as an array of characters. The difference between a character array and a string is the string is terminated with a special character '\0'.

Ways To Create String

const char *colour[4] = {"Blue","Red","Orange","Yellow"}; // using pointers
char colour[4][10] = {"Blue","Red","Orange","Yellow"}; // using 2D array
string colour[4] = {"Blue","Red","Orange","Yellow"}; // using string class
vector<string> colour {"Blue","Red","Orange","Yellow"}; // using vector class
array<string,4> colour {"Blue","Red","Orange","Yellow"}; // using array class

String Constructors

string str1;
string str2("initial string"); // initial string
string str3(6, '*'); // ******
string str4("first string", 6, 6); // string
string str5(str2.begin(), str2.begin() + 5);

String Functions

Input Functions

getline(cin, str); // get string value from input stream
push_back('c'); // input character at the end of string

Capacity Functions

pop_back(); // delete last character from string(c++11)
capacity(); // return the capacity allocated to string
resize(2); // change size of string, inc or dec
length(); // find the length of string
shrink_to_fit(); // dec the capacity of string to minimum capacity

Iterator Functions

begin(); // iterator to the start of string
end(); // iterator to the end of string
rbegin(); // reverse iterator to the end of string
rend(); //  reverse iterator to the start of string

Conversion Function

stoi(); // convert string to int(c++11)
atoi(); // convert string to int
stod(); // convert string to double
to_string(12); // convert int to string

Other Functions

copy("string", 2, 1); // target array, length, starting position
swap("str1","str2"); // swap one string with other
size(); // return length of string
clear(); // empty the string
[0]; // get character at position of string
at(0); // get character at position of string
back(); // last character of string
front(); // first character of string
append("str"); // append to the string
assign("str") // assign content to string
insert(0,"value"); // insert at position of string
erase(4,1); // position, number of characters to erase
find("st",0); // string to find, startin position
rfind("st") // find the position of last occurence of string/character
substr(0,3); // starting position(inclusive), ending position(exclusive)
compare(str,str1) // compare two strings
npos // maximum value of size_t