C++—STL學習筆記



#include<circle.h>用尖括號括起來時,編譯系統會到C++系統所在的目錄下去找該文件,如果找不到就出錯。#include“circle.h”用雙撇號是,編譯系統先到用戶當前目錄下去找該文件,如果找不到,再到C++系統所在的目錄下去找



#include<iostream>


#include<vector>
#include<algorithm>
#include<Algorithm>


using namespace std;


/*void main11(){  //容器:把你的元素copy到容器中


vector<int > v1;
v1.push_back(-1);
v1.push_back(1);
v1.push_back(3);
v1.push_back(5);




//迭代器:相當於一個指針
for (vector<int>::iterator it = v1.begin(); it != v1.end(); it++){  //定義一個迭代器


cout << *it << endl;


}




//算法:算法和迭代器進行無縫隙的鏈接
int num = count(v1.begin(), v1.end(), 3);
cout << "num:" << num << endl;




}*/


class Teacher{

public:
int age;
//char name[64];


public:
void ptintT(){
cout << "age:" << age << endl;
}


};






//容器中裝基礎類型變量
void main2(){


Teacher t1, t2, t3;
t1.age = 31;
t2.age = 32;
t3.age = 33;


vector<Teacher > v1;//容器:把你的元素copy到容器中  //容器實現了 數據類型和算法的有效分離,它可以加任何類型
v1.push_back(t1);
v1.push_back(t2);
v1.push_back(t3);


/*v1.push_back(-1);
v1.push_back(1);
v1.push_back(3);
v1.push_back(5);
*/


for (vector<Teacher>::iterator it = v1.begin(); it != v1.end(); it++){  //定義一個迭代器


cout << it->age << endl;


}




//算法:算法和迭代器進行無縫隙的鏈接
// int num = count(v1.begin(), v1.end(), 3);
// cout << "num:" << num << endl;








}


void main3(){


Teacher t1, t2, t3;
t1.age = 31;
t2.age = 32;
t3.age = 33;


Teacher *p1, *p2, *p3;
p1 = &t1;
p2 = &t2;
p3 = &t3;


vector<Teacher *> v1;//容器:把你的元素copy到容器中  //容器實現了 數據類型和算法的有效分離,它可以加任何類型
v1.push_back(p1);
v1.push_back(p2);
v1.push_back(p3);




for (vector<Teacher *>::iterator it = v1.begin(); it != v1.end(); it++){  //定義一個迭代器


cout << (*it)->age << endl;  //指向指針的指針,是二級指針


}     //????????????二級指針,多看下




}


void main(){
//vector<int> v1;


//main2();
main3();
//main11();
cout << "hello.." << endl;
system("pause");
return;




}






1.auto b=v.begin();
  或者:vector<int>::iterator it = v1.begin()  //每個容器類定義了一個名爲iterator的類型,該類型支持迭代器概念所規定的一套操作




2.vector 對應 數組
deque  對應 雙端數組
list 對應 雙向鏈表


set   集合
Map   鍵值對




















   一、  字符串---string


1.string初始化的幾種方法:


string s1 = "aaaaa";
string s2("bbbbb");
string s3 = s2;  //通過拷貝構造函數,來初始化對象s3
string s4(10, 'a');










#include<iostream>
#include<string>
using namespace std;


void main22(){


string s1 = "aaaaa";
string s2("bbbbb");
string s3 = s2;  //通過拷貝構造函數,來初始化對象s3
string s4(10, 'a');
}
void main33(){


string s1 = "aaaaadddd";
for (int i = 0; i < s1.length(); i++){
cout << s1[i] << " ";


for (string::iterator it = s1.begin(); it != s1.end(); it++)
{
cout << *it << endl;
}
}


}


void main(){
main33();
cout << "hello..." << endl;
system("pause");
return;


}




看到 4.04   10:37




#include<iostream>
#include<string>
#include<cstdio>
#include<algorithm>
//#include<stdio.h>
using namespace std;


void main22(){


string s1 = "aaaaa";
string s2("bbbbb");
string s3 = s2;  //通過拷貝構造函數,來初始化對象s3
string s4(10, 'a');
}
void main33(){


string s1 = "aaaaadddd";


//1 數組方式
for (int i = 0; i < s1.length(); i++){
cout << s1[i] << " ";

//2 迭代器
for (string::iterator it = s1.begin(); it != s1.end(); it++)
{
cout << *it << "  ";
}
}


try{
for (int i = 0; i < s1.length(); i++){


cout << s1.at(i) << " ";//at()與s1[]相比可以拋出異常 
}
}
catch (...){
cout << "拋出異常\n";
}


}


void main233(){


string s1 = "aaabbbccc";  //1、 char* ====>s1 把指針類型轉化爲string
//printf("s1:%s \n",s1.c_str());
// pritf();


// cout << "s1.c_str()==" << s1.c_str();  //2、 s1.c_str()   s1====>char* 把string類型轉化爲指針


//3、 s1的內容copy到buf中

char buf1[128] = { 0 };
//s1.copy(buf1,3,0);
cout << "buf:" << buf1 << endl;


}


void main25(){
string s1 = "dfa fs a df fs sdfd";
int index = s1.find("a", 0);  //位置下標從 0 開始
//while (index!=)


string::iterator z = s1.begin();
string::iterator  it = find(s1.begin(), s1.end(), 'd');  //刪除 d 字符  //????? //find()函數返回迭代器的位置
if (it != s1.end()){
s1.erase(it); //把迭代器所指的位置的數據刪除
}
cout << "s1刪除以後的結果:" << s1 << endl;
//把小寫換成大小 repalce();
s1.replace(0,2,"wwwwwwww");
cout << "repalce==="<<s1 << endl;




cout << "index:" << index << endl;
while (index != string::npos){    //相當於!= -1 ?  案例1、求a出現的次數,每一次出現的數組下標
cout << "" << index << endl; 
index = index + 1;
index=s1.find("a",index);
}


}






void main(){



main25();
// main233();
//main33();
cout << "hello..." << endl;
system("pause");
return;


}










容器二、stack ---棧     特點:先入的數據後出來,後入先出




#include<iostream>
#include<stack>
using namespace std;




class Teacher{


public:
int age;
char name[30];


public:
void printT(){
cout << "age:" << age << "      ";
}
};


void main2(){


Teacher t1, t2, t3;
t1.age = 21;
t2.age = 22;
t3.age = 23;


stack<Teacher > T;
T.push(t1);
T.push(t2);
T.push(t3);


while (!T.empty()){

Teacher tmp = T.top();  //是T.top()這一整個對象(包括 age,和printT()函數全部複製給 tmp對象 ?)

// cout << t1.age << "   " << t2.age << "   " << t3.age << endl;
T.top().printT();
//tmp.printT();
T.pop();
}


}




void main1(){
int i;
stack<int > s;
// for( i = 0; i < 10; i++ ){
// s.push(i + 1);
//}




for (i = 0; i < 10; i++){
s.push(i + 1);
}


while (!s.empty()){


int tmp = s.top();
cout << tmp << "  ";
s.pop();
}
}


int main(){



main2();
//main1();



system("pause");
return 0;
}












3.


for (vector<int >::reverse_iterator rit = v.rbegin(); rit != v.rend(); rit++){
cout << "*it===" << *rit << "   ";
}










// 刪除單個數  *********重要***********


vector<int> v = {1,2,3,3,2,1,1,2,3,2};
for (auto i = v.begin(); i != v.end();){


if (*i == 2){
i=v.erase(i);  //當 刪除迭代器所指向的元素的時候,erase刪除函數會讓i自動下移
}
else{
i++;


}


}


















4.
int a = 3;
const int &t1 = a;  


const int &t =4;  //常引用是可以直接賦值的




5.
// TEMPLATE STRUCT greater
template<class _Ty = void>
struct greater
: public binary_function<_Ty, _Ty, bool>
{ // functor for operator>
bool operator()(const _Ty& _Left, const _Ty& _Right) const
{ // apply operator> to operands
return (_Left > _Right);
}
};






sort(v.begin(), v.end(), greater<string>());


並不是一定只有greater類對象纔可以調用重載()函數,因爲greater也是繼承於其他函數,所有最後 v中 的對象也可以調用   ???






6. 函數適配器:
  equal_to<string>() 有兩個參數left參數來自容器,right參數來自sc
//bind2nd函數適配器,把預定義函數對象和第二個參數進行綁定


int num=count_if(v.begin(),v.end(),bind2nd(equal_to<string>(),sc))






7.兩種方式:






void showElemt(const int &n){      
cout << n << "  ";          //回調函數
}


class SMyshow{
public:
void operator() (int &n){
cout << n << "   ";   //函數對象
}


};




void main41(){
vector<int> v;
v.push_back(1);
v.push_back(2);
v.push_back(3);
printV(v);
cout << endl;
for_each(v.begin(), v.end(), showElemt);
for_each(v.begin(), v.end(), SMyshow()); 


//SMyshow() 是匿名函數對象    
//相當於
        SMyshow a;
SMyshow t = for_each(v.begin(), v.end(), a);


 
}








7.for_each()  和transform() 的回調函數是不同的


for_each() 回調函數:可以沒有返回值,形參可以是引用










transform() 的回調函數:  










6.構造函數即使是空的也一定要加上函數體!!!
如 Shape(){ }  這樣纔是正確的(錯誤寫法:Shape();) 










// Circle circle(5);
// Point *p=&circle;  //分配的是靜態內存,系統自動釋放

Point *p=new Circle(5);
delete p;        //delete一定和new匹配 ,new分配的是動態內存,一定要用delete手動釋放







發佈了34 篇原創文章 · 獲贊 68 · 訪問量 18萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章