動態數組
using namespace std;
#include<vector>
vector<T>vec;//C++中直接構造一個動態數組的方法
用動態數組存儲自定義類型
struct Student {
string name; // 名字
int age; // 年齡
};
int main() {
vector<Student> class1; // 班級
Student stu1, stu2; // 學生1,學生2
stu1.name = "xiaohong";
stu1.age = 12;
stu2.name = "yuhaoran";
stu2.age = 25;
class1.push_back(stu1);
class1.push_back(stu2);
return 0;
}
動態數組的構造函數
int n = 10;
//默認的初始值爲0
//定義一個初始值爲0 的長度爲n的動態數組tys
vector<int>tys(n);
//定義一個初始值爲1 的長度爲n的動態數組vec
vector<int> vec(n, 1);
上面這段代碼說明了創建一個長度爲n的初始值全爲1的動態數組。
二位動態數組
int n = 5;
vector<vector<int> > vec2;
for (int i = 0; i < n; i++) {
vector<int> x(n, 1); //必須一維一維的賦值
vec2.push_back(x);
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < vec2[i].size(); i++) {
cout << vec2[i][j] << " ";
}
cout << endl;
}
構造一個n行m列的動態數組
//vector<vector<int> >vec(n,vector<int>(m,0));
int main(){
int n= 5,m = 5;//定義5行5列動態數組 打印輸出
vector<vector<int> >vec(n,vector<int>(m,0));
for(int i=0;i<vec.size();i++){
for(int j=0;j<vec[0].size();j++)
cout<<vec[i][j]<<" ";
cout<<endl;
}
return 0;
}
二維動態數組的使用
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<vector<int> > v2d;//定義一個二維動態數組
for (int i = 0; i < 5; i++) {
v2d.push_back(vector<int>());//初始化二維數組 每一維都是一個空的vector<int>()
}
for (int i = 0; i < v2d.size(); i++) {
for (int j = 0; j <= i; j++) {
v2d[i].push_back((i + 1) * (j + 1)); //第j行push進去j個數
}
}
for (int i = 0; i < v2d.size(); i++) { //打印輸出
for (int j = 0; j < v2d[i].size(); j++) {
cout << i + 1 << " * " << j + 1 << " = " << v2d[i][j] << "\t";
}
cout << endl;
}
return 0;
}
集合set
構造一個集合
using namespace std;
#include<set>
set<int>myset;//初始化的時候 set是一個空集合
set的主要用法
#include <iostream>
#include <string>
#include <set>
using namespace std;
int main() {
set<string> country;
country.insert("China");
country.insert("America");
country.insert("France");
set<string>::iterator it;
for (it = country.begin(); it != country.end(); it++) {
cout << *it << " ";
}
cout << endl;
country.erase("America");
country.erase("England");
if (country.count("China")) {
cout << "China in country." << endl;
}
country.clear();
return 0;
}
C++中set是從小到大遍歷的,set會自動排序
默認數據類型是有大小規則的 set可以自動排序
對於用戶自定義的數據類型,就需要用到運算符重載
struct Node {
int x, y;
//比較重要的一個知識點 內置比較函數
bool operator<(const Node &rhs) const {
if (x != rhs.x)
return x < rhs.x;
else
return x < rhs.x;
}
};
映射表map
map表示的是key到value的映射關係
key的集合稱爲鍵的集合,value的集合稱爲值的集合
構造一個映射
#include<map>
using namespace std;
map<stirng,int>types;
在C++中map的遍歷也是按照鍵的從小到大遍歷的
map的使用
#include <iostream>
#include <string>
#include <map>
using namespace std;
int main() {
map<string, int> dict;
dict["Tom"] = 1; // or write as dict.insert(make_pair("Tom",1));
dict["Jone"] = 2;
dict["Mary"] = 1;
if (dict.count("Mary")) {
cout << "Mary is in class " << dict["Mary"];
dict["Mary"] = 5;
}
//遍歷map 這裏迭代器指向的元素是一個pair,有first和second兩個成員變量,分別代表一個映射
for (map<string, int>::iterator it = dict.begin(); it != dict.end(); it++) {
cout << it->first << " is in class " << it->second << endl;
}
dict.clear();
return 0;
}
難點:二維map
#include <iostream>
#include <map>
#include <string>
using namespace std;
int main() {
//定義一個二維map "> >"之間一定要有一個空格
map<int, map<string, int> > info;
int n; cin >> n;
for (int i = 0; i < n; i++) {
int class_id; string name;
cin >> class_id >> name;
info[class_id][name]++;
}
for (map<int, map<string, int> >::iterator it1 = info.begin(); it1 != info.end(); it1++) {
for (map<string, int>::iterator it2 = it1->second.begin(); it2 != it1->second.end(); it2++) {
cout << "There are " << it2->second << " people named " << it2->first << " in class " << it1->first << endl;
}
}
return 0;
}
/*
6
1 liu
2 bai
2 liu
3 yan
4 yan
1 bai
There are 1 people named bai in class 1
There are 1 people named liu in class 1
There are 1 people named bai in class 2
There are 1 people named liu in class 2
There are 1 people named yan in class 3
There are 1 people named yan in class 4
*/
打印鋸齒矩陣
#include<iostream>
#include<vector>
using namespace std;
int main(){
int n,m;cin>>n>>m;
vector<vector<int> >a(n,vector<int>());//初始化一個具有n行的動態數組
int x,y;
for(int i=1;i<=m;i++){
cin>>x>>y;
a[x-1].push_back(y);
}
for(int i=0;i<a.size();i++){
//如果某行沒有任何元素 則輸出一個空行
if(a[i].size() == 0){
cout<<endl;
continue;
}
for(int j=0;j<a[i].size()-1;j++) cout<<a[i][j]<<" ";
cout<<a[i][a[i].size()-1]<<endl;
}
return 0;
}
蒜頭君破案
#include<iostream>
#include<set>
using namespace std;
typedef struct person{ //定義一個結構體 存放每個人的身高 體重 年齡
int x,y,z;
//運算符重載 set裏面自動排序 結構體必須定義一個排序算子
bool operator<(const person &p) const{
if(x != p.x) return x < p.x;
else if(y != p.y) return y < p.y;
else return z < p.z;
}
}person;
int main(){
set<person>city;//定義一個person結構體的集合
int n,m;cin>>n>>m;
int x,y,z;
for(int i=1;i<=n;i++){
person a;cin>>a.x>>a.y>>a.z;
city.insert(a);//插入到集合中
}
for(int i=1;i<=m;i++){
person b;
cin>>b.x>>b.y>>b.z; //判斷b是否在集合city中
if(city.count(b)) cout<<"yes"<<endl;
else cout<<"no"<<endl;
}
return 0;
}
蒜頭君的藏書
#include<iostream>
#include<map>
using namespace std;
int main(){
map<string, int> T;//建立一個書名到數量的映射關係
int n;cin>>n;string b;
for(int i=1;i<=n;i++){
cin>>b;T[b]++;
}
cout<<T.size()<<endl;
//注意遍歷map的技巧 it != T.end()
for(map<string, int>::iterator it = T.begin();it != T.end();it++){
cout<<it->first<<" "<<it->second<<endl;
}
return 0;
}