Strings vs CStrings Flashcards
String Constructors
default (1) string();
copy (2) string (const string& str);
substring (3) string (const string& str, size_t pos, size_t len = npos);
from c-string (4) string (const char* s);
from buffer (5) string (const char* s, size_t n);
fill (6) string (size_t n, char c);
range (7) template string (InputIterator first, InputIterator last);
initializer list (8) string (initializer_list il);
move (9) string (string&& str) noexcept;
Function of:
- stof
- stold
- stoi
- strtof
- Convert string to float
- String to long double
- string to int
- ?
include-ing the two
C-strings (#include <cstring>)</cstring>
C++ strings (#include <string> )</string>
- Declaring a C-string variable
- Declaring a C++ string object
- char str[10];
- string str;
Initializing a C-string variable
char str1[11] = “Call home!”;
char str2[] = “Send money!”;
char str3[] = {‘O’, ‘K’, ‘\0’};
Last line above has same effect as:
char str3[] = “OK”;
Initializing a C++ string object
string str1(“Call home!”);
string str2 = “Send money!”;
string str3(“OK”);
Assigning to a C-string variable
Can’t do it, i.e., can’t do this:
char str[10];
str = “Hello!”;
Assigning to a C++ string object
string str;
str = “Hello”;
str = otherString;
Concatenating two C-strings
strcat(str1, str2);
strcpy(str, strcat(str1, str2));
Concatenating two C++ string objects
str1 += str2;
str = str1 + str2;
Copying a C-string variable
char str[20];
strcpy(str, “Hello!”);
strcpy(str, otherString);
Copying a C++ string object
string str;
str = “Hello”;
str = otherString;
Accessing a single character (cstring)
str[index]
Accessing a single character(Strinng Obj)
str[index]
str.at(index)
str(index, count)
]