array 陣列
陣列一個基礎的資料結構,為一種靜態的資料結構(static data
structure),在使用前須要先宣告大小。 1
2
3
4int main(){
int arr[10] = {1,2,3,4,5,6,7,8,9,10};
return 0;
}1
2
3
4
5
6
7
8
9
10int main(){
int arr[10];
for(int i = 0;i < 10;i++)
arr[i] = i*10;
for(int i = 0;i < 10;i++)
cout << arr[i] << " ";
cout << endl;
return 0;
}1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
using namespace std;
int main(){
int * arr = new int[10];//動態配置記憶體
for(int i = 0;i < 10;i++)
arr[i] = i;
for(int i = 0;i < 10;i++)
cout << arr[i] << " ";
delete [] arr;//將記憶體歸還
cout << '\n' << "==============" << endl;
for(int i = 0;i < 10;i++)
cout << arr[i] << " ";
return 0;
}1
2
3
4
5
6
7
8
using namespace std;
int main(){
vector<int> vec;
return 0;
}1
2
3
4
5
6
7
8
9
10
11
12
13
using namespace std;
int main(){
vector<int> vec;
vec.reserve(128);
for(int i = 0;i < 10;i++)
vec.push_back(i);
for(int i = 0;i < vec.size();i++)
cout << vec[i] << " ";//ouput = 0 1 2 3 4 5 6 7 8 9
return 0;
}1
2
3
4
5
6
7
8
9
10
11int main(){
vector<int> vec;
vec.reserve(128);
for(int i = 0;i < 10;i++)
vec.push_back(i);
vec.insert(vec.begin()+3,1000);
for(int i = 0;i < vec.size();i++)
cout << vec[i] << " ";//output = 0 1 2 1000 3 4 5 6 7 8 9
return 0;
}1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
using namespace std;
int main(){
vector<int> vec;
vec.reserve(128);
for(int i = 0;i < 10;i++)
vec.push_back(i);
vec.erase(vec.begin()+1);
vec.erase(vec.begin()+3,vec.begin()+6);
for(int i = 0;i < vec.size();i++)
cout << vec[i] << " ";//output = 0 2 3 7 8 9
return 0;
}1
2
3
4
5
6
7
8
9
10
11
12
13
using namespace std;
int main(){
vector<int> vec;
for(int i = 0;i < 10;i++)
vec.emplace_back(i);
vec.emplace(vec.begin()+5,2021);
for(int i = 0;i < vec.size();i++)
cout << vec[i] << " ";
return 0;
}1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
using namespace std;
int main(){
vector<pair<int,char>> vec;
for(int i = 0;i < 10;i++)
vec.emplace_back(make_pair(i,65+i));
for(int i = 0;i < vec.size();i++)
cout << vec[i].first << " " << vec[i].second << endl;
/*ouput:
0 A
1 B
2 C
3 D
4 E
5 F
6 G
7 H
8 I
9 J
*/
return 0;
}1
2
3
4
5
6
7
8
9
10
11
12
13
14
using namespace std;
int main(){
queue<int> q;
for(int i = 0;i < 10;i++)
q.push(i);
for(int i = 0;i < 10;i++){
cout << q.front() << " ";
q.pop();
}
return 0;
}1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
using namespace std;
int main(){
queue<int> q;
q.emplace(1);
q.emplace(10);
q.emplace(100);
auto l = q.size();
for(int i = 0;i < l;i++){
cout << q.front() << endl;
q.pop();
}
return 0;
}1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
using namespace std;
int main(){
stack<int> s;
for(int i = 0;i < 10;i++)
s.push(i);
auto l = s.size();
for(auto i = 0;i < l;i++){
cout << s.top() << " ";
s.pop();
}
return 0;
}1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
using namespace std;
int main(){
set<int> my_set;
my_set.insert(100);
my_set.insert(200);
my_set.insert(300);
my_set.erase(100);
cout << my_set.count(100) << endl;//不存在輸出0
cout << my_set.count(200) << endl;//存在輸出1
return 0;
}1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
using namespace std;
int main(){
map<int,string> m;
m[1] = "one";
m[2] = "two";
m[3] = "three";
m[4] = "four";
cout << m.count(1) << endl;//存在輸出1
cout << m.count(5) << endl;//存在輸出0
cout << m[2] << endl;//output = two
cout << m[3] << endl;//output = three
return 0;
}