#include <string>
int main()
{
using namespace std;
char charr1[20];
char charr2[20] = "jaguar";
string str1;
string str2 = "panther";
cout << "Enter a kind of feline: ";
cin >> charr1;
cout << "Enter another kind of feline: ";
cin >> str1;
cout << "Here are some felines:\n";
cout << charr1 << " " << charr2 << " "
<< str1 << " " << str2 << endl;
cout << "The third letter in " << charr2 << " is "
<< charr2[2] << endl;
cout << "The third letter in " << str2 << " is "
<< str2[2] << endl;
return 0;
}
The mechanism of string operation is different to the C-style. Although you can initialize a string object to a C-style string.In this program tell us some facts we need to know. Cin can store keyboard input in a string object, just as the definition of string. "cout" can directly display a string object. If you like another way, okay, we can display the string like the character array. the irreversible behavior of C-style. I think that's important to know how to store the string in C++ circumstance?
#include <iostream>
#include <string>
int main()
{
string str2 = "Hello"
cout << "The size of str2: " << sizeof str2 << endl;
cout << "The second letter in str2: " << str2[1] << endl;
return 0;
}
The pointer behavior: 4 bytes. ?? the same concept in python... (Acctually, we need more information about it)
the next process tell me. str.size() return the length of string as the same as the strlen(str).
Noting in my mind right now.. continue...
The books says:
Conceptually, one thinks of an array of char as a collection of char storage units used to store a string but of a string class variable as a single entity representing the string.
C++98:
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 sequence (5) | string (const char* s, size_t n); |
fill (6) | string (size_t n, char c); |
range (7) | template <class InputIterator> string (InputIterator first, InputIterator last); |
c++11:
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 <class InputIterator> string (InputIterator first, InputIterator last); |
initializer list (8) | string (initializer_list<char> il); |
move (9) | string (string&& str) noexcept; |