今天又来了,把今天学到的一些东西记录一下,也方便大家给我指出不足
/*
遍历算法 遍历容器元素
@param beg 开始迭代器
@param end 结束迭代器
@param _callback 函数回调或者函数对象
@return 函数对象
*/
void dust(int num){ //回调函数
cout << num << endl;
}
class dust01{
public:
void operator()(int num){
cout << num << endl;
}
};
//类也可以用结构体替换,其目的也不用明说了,就是~~~偷懒
struct dust01{
void operator()(int num){
cout << num << " ";
count++;
}
int count = 0; //统计调用次数
};
void test01(){
vector<int>p;
for (int i = 0; i < 10; i++){
p.push_back(i);
}
//for_each()返回是数据类型
dust01 printf = for_each(p.begin(), p.end(), dust01()); //为了打印 dust02()匿名对象用来进行初始化
cout << "operator调用次数" << printf.count << endl;
}
struct dust02:public binary_function<int,int,void>{
void operator()(int num,int val) const {
cout << num + val << " ";
}
};
void test02(){
vector<int>p;
for (int i = 0; i < 10; i++){
p.push_back(i);
}
//for_each()返回是数据类型
for_each(p.begin(), p.end(), bind2nd(dust02(),100)); //为了打印 dust02()匿名对象用来进行初始化
}
transform算法 将指定容器区间元素搬运到另一容器中
注意:transform 不会给目标容器分配内存,所以需要我们提前分配好内存
@param beg1 源容器开始迭代器
@param end1 源容器结束迭代器
@param beg2 目标容器开始迭代器
@param _callback 函数回调或者函数对象
@return 返回目标容器迭代器
class Transform{
public:
int operator()(int val1){
return val1 + 10 ;
}
};
void test03(){
vector<int>p;
for (int i = 0; i < 10; i++){
p.push_back(i);
}
vector<int>p1; //记得容器之间的转移需要开辟空间
p1.resize(p.size());
transform(p.begin(), p.end(), p1.begin(), Transform());
for_each(p1.begin(), p1.end(), [](int val){cout << val << " "; });
}
class Transform02{
public:
int operator()(int val1,int val2){
return val1 + val2;
//0 2 4 6 8 10 12 14 16
}
};
void test04(){
vector<int>p2;
vector<int>p3;
for (int i = 0; i < 10; i++){
p2.push_back(i); //0 1 2 3 4 5 6 7 8 9
p3.push_back(i); //0 1 2 3 4 5 6 7 8 9
}
vector<int>target;
target.resize(p2.size());
transform(p2.begin(), p2.end(), p3.begin(), target.begin(), Transform02());
for_each(target.begin(), target.end(), [](int val){cout << val << " "; });//利用bulangda表达式
//0 2 4 6 8 10 12 14 16
/*
find算法 查找元素
@param beg 容器开始迭代器
@param end 容器结束迭代器
@param value 查找的元素
@return 返回查找元素的位置
*/
/* 查找普通数据类型 */
void test01(){
vector<int>v;
for (int i = 0; i < 10; i++){
v.push_back(i);
}
vector<int>::iterator pos=find(v.begin(), v.end(), 5);
if (pos != v.end()){
cout << "找到了数据,为:" << *pos << endl;
}
else{
cout << "没有找到" << endl;
}
}
class Person{
public:
Person(string name,int age){
this->m_name = name;
this->m_age = age;
}
bool operator==(const Person& p){
if (this->m_name == p.m_name&&this->m_age == p.m_age){
return true;
}
else{
return false;
}
}
string m_name;
int m_age;
};
/* 查找自定义数据类型 */
void test02(){
vector<Person>p;
Person p1("aaa", 10);
Person p2("bbb", 20);
Person p3("ccc", 30);
Person p4("ddd", 40);
p.push_back(p1);
p.push_back(p2);
p.push_back(p3);
p.push_back(p4);
vector<Person>::iterator pos = find(p.begin(), p.end(), p2);
if (pos != p.end()){
cout << "姓名:" << pos->m_name << ",年龄:" << pos->m_age << endl;
}
}
/* adjacant_find找重复位置 */
void test04(){
vector<int>p;
p.push_back(1);
p.push_back(2);
p.push_back(3);
p.push_back(5);
p.push_back(5);
vector<int>::iterator pos=adjacent_find(p.begin(), p.end());
if (pos != p.end()){
cout << "找到了重复的位置,数据为:" << *pos << endl;
}
else{
cout << "没有找到" << endl;
}
}
/* count 计数的使用 */
void test05(){
vector<int>p;
p.push_back(1);
p.push_back(2);
p.push_back(3);
p.push_back(5);
p.push_back(5);
int num = count(p.begin(), p.end(), 5);
cout << "5的个数:" << num << endl;
}
class greaterThen{
public:
bool operator()(int v){
return v > 4;
}
};
void test06(){
vector<int>p;
p.push_back(1);
p.push_back(2);
p.push_back(3);
p.push_back(5);
p.push_back(5);
int pos =count_if(p.begin(), p.end(),greaterThen() );
cout << "大于4的次数:" << pos << endl;
}
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
今天分享到此结束,我的代码因为刚开始学,所以有很多不足,码的一些字有有很多瑕疵,希望大家能指出我的不足,我可以在以后的博客中进行改正。
小白在此谢谢大家能有耐心的看到我的这句话~~~~~~~~~~~~~~~~~~~~