String and vector are two standard library types. The former supports variable-length strings, and the latter supports variable-length collections. Built-in arrays are more basic types, and string and vector are some abstractions of it.
1. string
Call the string library:
#include <string>
using std::string
Five forms defined by string:
#include <iostream>
#include <string>
using namespace std;
int main(){
string s1 = "Hello";//拷贝初始化
string s1_copy = s1;//拷贝
string s2(5,'a');//直接初始化
string s2_copy(s2);//拷贝
string s3("Hello world!");//直接初始化
cout << s1 << endl;
cout << s1_copy << endl;
cout << s2 << endl;
cout << s2_copy<< endl;
cout << s3 << endl;
}
result:
Hello
Hello
aaaaa
aaaaa
Hello world!
string operation
operating | meaning |
---|---|
getline(is,s) | Read a line from is and assign it to s |
s.empty() | true if s is empty, false otherwise |
s.size() | s characters |
s[n] | Character with index n |
s1 + s2 | The result of connecting s1 and s2 |
s1 == s2 | Determine if they are equal |
s1! = s2 | Determine if they are not equal |
<,<=,>,>= | Lexicographical comparison |
Read and write string objects :
When reading, string automatically ignores the leading white space (space character, new line character, tab character, etc.), starting from the real character until it encounters the next white space.
#include <iostream>
#include <string>
using namespace std;
int main(){
string s1;
cin >> s1;
cout << s1 << endl;
}
result:
Hello world
Hello
Reading an unknown number of string objects The
input stream has been reading.
#include <iostream>
#include <string>
using namespace std;
int main(){
string word;
while(cin >> word){
cout << word << endl;
}
}
result:
Hello world we are champion// 输入
Hello
world
we
are
champion
Use getline to read an entire line
so that the space character can be read.
#include <iostream>
#include <string>
using namespace std;
int main(){
string line;
while(getline(cin,line)){
cout << line << endl;
}
return 0;
}
endl
End the current line and refresh the display buffer.
result:
we are champion//输入
we are champion
are you ok//输出
are you ok
2. vector
Vector represents a collection of objects, and vector is also called a container.
#include <vector>
using std::vector;
Vector 5 initialization methods:
#include <iostream>
#include <vector>
using namespace std;
void print_vector(vector<auto> vec){
for (auto i:vec){
cout << i << " ";
}
cout << endl;
}
int main(){
vector<int> v1(5,10);//5个重复元素,值为10
vector<int> v1_copy(v1);
vector<int> v2{1,2,3,4,5};
vector<int> v2_copy = v2;
vector<int> v3={1,2,3,4,5};
print_vector(v1);
print_vector(v1_copy);
print_vector(v2);
print_vector(v2_copy);
print_vector(v3);
}
result:
10 10 10 10 10
10 10 10 10 10
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
If you simply declare the definition vector<T> vec
, it is initialized by default and vec
contains no elements.
If you can't compile, you need to set the compiler's compilation standard to ISO C++11
.Dev C ++ 's Tools-Compiler Options-Code Generation-Language Standard.
If you traverse the process, you want to vector
change the value of:
for(auto &v : vec){
v *= v;
}
vector operation
operating | meaning |
---|---|
push_back() | Add element |
v.empty() | Determine if it contains elements |
v.size() | Number of elements |
v [n] | Take the value at index n |
v1 = v2 | Replace elements of v1 with elements from v2 (same container type) |
v1 == v2 | Determine if they are equal |
v1 != v2 | Determine if they are not equal |
<,<=,>,>= | Lexicographical comparison |
Vector access:
#include <iostream>
#include <vector>
using namespace std;
void print_vector(vector<auto> vec){
for (auto i:vec){
cout << i << " ";
}
cout << endl;
}
int main(){
vector<float> v{1,2,3,4,5};
for (auto &i : v){// i是一个引用
i *= i;
}
print_vector(v);
}
3. Array
The size of the array is fixed.
Array initialization:
#include <iostream>
using namespace std;
int main(){
int a[5] = {0,1,2};
string s[5] = {"hi","world"};
for(auto i:a){
cout<< i<< endl;
}
}
result:
0
1
2
0
0
Pointers and arrays:
#include <iostream>
using namespace std;
int main(){
int arr[5] = {0,1,2};
int *ip = arr;// 等价于int *p = &arr[0]
int *ip2 = ip+2;//ip2指向arr[2]
cout << *ip << endl;
cout << *ip2 << endl;
}
result:
0
2
Multidimensional Arrays:
#include <iostream>
using namespace std;
int main(){
int arr[2][3] = {
{0,1,2},
{3,4,5},
};
for (auto &row : arr){
for (auto &col : row){
cout << col << endl;
}
}
}
result:
0
1
2
3
4
5