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手动释放







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值