目錄
一、set集合
1.1 使用
#include<set>
set頭文件可以保證輸入不重複,輸出爲0到15
int main(){
Solution Solution;
int num; num = 10;
set<int> int_set;
while (num--){
int_set.insert(num);
}
for (int idx = 0; idx < 15; idx++){
int_set.insert(idx);
}
for (auto item : int_set){
cout << item << " ";
}
int end; cin >> end;
return 0;
}
// 輸出 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
1.2 插入與遍歷
插入用.insert,
遍歷直接可以auto 即可
二、list
https://blog.csdn.net/leo_888/article/details/80772146
c++中的list會被初始化爲雙向鏈表。可以實現方便的插入與刪除。
但是不能取地址,比如下面這種操作是不行的:報錯,除非將a初始化爲vector
//錯誤,list無法隨機訪問
list<int> a;
for (int idx = 0; idx < 5; idx++){
cout << a[idx] << " ";
}
//錯誤,list相當於雙向鏈表,無法用<
for (auto iter = a.begin(); iter < a.end();iter++){
cout << *(a.begin()) << " ";
}
//正確,只能一次次往下迭代
for (auto iter = a.begin(); iter != a.end();iter++){
cout << *(a.begin()) << " ";
}
2.1 常用插入與刪除
push_back與push_front
int main(){
list<int> a;
for (int idx=0; idx < 10; idx++){
a.push_back(idx);
a.push_front(6);
}
for (auto item: a){
cout << item << " ";
}
cout << endl;
while (*(a.begin()) == 6){
a.pop_front();
}
for (auto item : a){
cout << item << " ";
}
cout << endl;
int end; cin >> end;
return 0;
}
輸出結果:
6 6 6 6 6 6 6 6 6 6 0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8 9
三、類的初始化
3.1 看程序說結果
class A
{
private:
int n1;
int n2;
public:
A() :n2(0), n1(n2 + 2)
{}
void Print(){
std::cout << n1<<" "<< n2 << std::endl;
}
};
int main(){
A a;
a.Print();
A* ptr = new A();
ptr->Print();
int end; cin >> end;
return 0;
}
運行結果:
輸出:
-858993458 0
-842150449 0
原因:
- 類中的初始化順序只與類中聲明順序有關
即按照下面順序,先初始n1,再初始n2
private:
int n1;
int n2;
所以先用n2+2初始化n1,是隨機的,然後再用0初始化n2。因此得到上面輸出
3.2 類初始化順序
變量初始化順序:https://blog.csdn.net/weixin_40087851/article/details/82346473
· 從全局看,變量的初始化順序如下:
- 基類的靜態變量或全局變量。
- 派生類的靜態變量或全局變量。
- 基類的成員變量。
- 派生類的成員變量。
四、auto變量
https://www.cnblogs.com/KunLunSu/p/7861330.html
4.1 C98中的auto
無用,可不看。瞭解即可。C++11完全捨棄了這種用法。
C++98標準中就存在了auto關鍵字,那時的auto用於聲明變量爲自動變量,自動變量意爲擁有自動的生命期,這是多餘的,因爲就算不使用auto聲明,變量依舊擁有自動的生命期:
int a =10 ; //擁有自動生命期
auto int b = 20 ;//擁有自動生命期
static int c = 30 ;//延長了生命期
4.2 C++11的auto
auto可以在聲明變量的時候根據變量初始值的類型自動爲此變量選擇匹配的類型,類似的關鍵字還有decltype。舉個例子:
int a = 10;
auto au_a = a;//自動類型推斷,au_a爲int類型
cout << typeid(au_a).name() << endl;
typeid運算符可以輸出變量的類型。程序的運行結果輸出了
int
4.3 應用實例
上面舉的這個例子很簡單,在真正編程的時候也不建議這樣來使用auto,直接寫出變量的類型更加清晰易懂。下面列舉auto關鍵字的正確用法。
用於代替冗長複雜、變量使用範圍專一的變量聲明。
想象一下在沒有auto的時候,我們操作標準庫時經常需要這樣:
#include<string>
#include<vector>
int main()
{
std::vector<std::string> vs;
for (std::vector<std::string>::iterator i = vs.begin(); i != vs.end(); i++)
{
//...
}
}
這樣看代碼寫代碼實在煩得很。有人可能會說爲何不直接使用using namespace std,這樣代碼可以短一點。實際上這不是該建議的方法(C++Primer對此有相關敘述)。使用auto能簡化代碼:
#include<string>
#include<vector>
int main()
{
std::vector<std::string> vs;
for (auto i = vs.begin(); i != vs.end(); i++)
{
//..
}
}
for循環中的i將在編譯時自動推導其類型,而不用我們顯式去定義那長長的一串。
在定義模板函數時,用於聲明依賴模板參數的變量類型。
template <typename _Tx,typename _Ty>
void Multiply(_Tx x, _Ty y)
{
auto v = x*y;
std::cout << v;
}
若不使用auto變量來聲明v,那這個函數就難定義啦,不到編譯的時候,誰知道x*y的真正類型是什麼呢?
模板函數依賴於模板參數的返回值
template <typename _Tx, typename _Ty>
auto multiply(_Tx x, _Ty y)->decltype(x*y)
{
return x*y;
}
當模板函數的返回值依賴於模板的參數時,我們依舊無法在編譯代碼前確定模板參數的類型,故也無從知道返回值的類型,這時我們可以使用auto。格式如上所示。
decltype操作符用於查詢表達式的數據類型,也是C++11標準引入的新的運算符,其目的也是解決泛型編程中有些類型由模板參數決定,而難以表示它的問題。
auto在這裏的作用也稱爲返回值佔位,它只是爲函數返回值佔了一個位置,真正的返回值是後面的decltype(_Tx*_Ty)。爲何要將返回值後置呢?如果沒有後置,則函數聲明時爲:
decltype(x*y)multiply(_Tx x, _Ty y)
而此時x,y還沒聲明呢,編譯無法通過。
4.4 auto注意事項
- auto 變量必須在定義時初始化,這類似於const關鍵字。
-
定義在一個auto序列的變量必須始終推導成同一類型。例如:
auto a4 = 10, a5 = 20, a6 = 30;//正確 auto b4 = 10, b5 = 20.0, b6 = 'a';//錯誤,沒有推導爲同一類型
-
如果初始化表達式是引用,則去除引用語義。
int a = 10; int &b = a; auto c = b;//c的類型爲int而非int&(去除引用) auto &d = b;//此時c的類型才爲int& c = 100;//a =10; d = 100;//a =100;
-
如果初始化表達式爲const或volatile(或者兩者兼有),則除去const/volatile語義。
const int a1 = 10; auto b1= a1; //b1的類型爲int而非const int(去除const) const auto c1 = a1;//此時c1的類型爲const int b1 = 100;//合法 c1 = 100;//非法
-
如果auto關鍵字帶上&號,則不去除const語意。
const int a2 = 10; auto &b2 = a2;//因爲auto帶上&,故不去除const,b2類型爲const int b2 = 10; //非法
-
初始化表達式爲數組時,auto關鍵字推導類型爲指針。
int a3[3] = { 1, 2, 3 }; auto b3 = a3; cout << typeid(b3).name() << endl;
程序將輸出
int *
-
若表達式爲數組且auto帶上&,則推導類型爲數組類型。
int a7[3] = { 1, 2, 3 }; auto & b7 = a7; cout << typeid(b7).name() << endl;
程序輸出
int [3]
-
函數或者模板參數不能被聲明爲auto
void func(auto a) //錯誤 { //... }
-
時刻要注意auto並不是一個真正的類型。
auto僅僅是一個佔位符,它並不是一個真正的類型,不能使用一些以類型爲操作數的操作符,如sizeof或者typeid。cout << sizeof(auto) << endl;//錯誤 cout << typeid(auto).name() << endl;//錯誤
1.5 個人應用
在vector名爲tmp中,如何遍歷?這種看上去更像是python的遍歷。(python中很多簡便的語法是從c++中引申過來的)
直接 for(auto it : tmp)即可表示it遍歷了tmp中的所有元素,非常簡便。也可以for( int it:tmp)更簡便,也可以。這點語法更類似於用it表示tmp中元素的個數,然後完成循環。循環的因子是int。此應用必須熟練掌握,非常簡便。
vector<int> tmp;
tmp.push_back(n);
for (int i = n / 2; i >= 1; i--){
if (n%i == 0 && m%i == 0)tmp.push_back(i);
}
for (auto it : tmp){
string ss = str_2.substr(0, it);
string A, B;
for (int k = 0; k<n / it; k++){
A += ss;
}
for (int k = 0; k<m / it; k++){
B += ss;
}
if (A == str_2 && B == str_1){
cout << ss; break;
}
}