//----------------------------------------------------------------------------------------
// Desc: STL search() used in vector container and struct data
// Author: spring brother
// Data: 2018.3.24
// Copyright (C) 2018 spring brother
//----------------------------------------------------------------------------------------
/*
for_each(beg,end,unaryOp)
unaryOp的返回类型为void,当unaryOp传入的参数为引用类型时,可修改元素的值
mismatch(beg1,end1,beg2)
mismatch(beg1,end1,beg2,binaryPred)
返回一个迭代器的pair,指明两序列中第一个不匹配元素的位置。如果所有元素都匹配,
则返回的pair中的第一个迭代器为end1,第二个迭代器指向beg2中偏移量等于第一个序列长度的位置。
注意:短序列在前,长序列在后。
equal(beg1,end1,beg2)
equal(beg1,end1,beg2,binaryPred)
确定两个元素是否相等,如果输入序列中的每个元素都与从beg2开始的序列中对应元素相等,则返回true。
注意:短序列在前,长序列在后。
*/
#include "stdafx.h"
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
void print(int &i) {
cout << i <<" ";
}
void modifiedValue(int &i) {
i = i * 2;
}
int main()
{
int arrIn[6] = { 1,2,4,4,4,5 };
vector<int> vecIn(arrIn, arrIn + 6);
//传入仿函数
for_each(vecIn.begin(), vecIn.end(), print); //for_each
cout << endl;
for_each(vecIn.begin(), vecIn.end(), modifiedValue);
for_each(vecIn.begin(), vecIn.end(), print);
cout << endl;
//传入匿名函数
for_each(vecIn.begin(), vecIn.end(), [](int &i) {
cout << i << " ";
});
cout << endl;
for_each(vecIn.begin(), vecIn.end(), [](int &i) {
i = i * 2;
});
for_each(vecIn.begin(), vecIn.end(), [](int &i) {
cout << i << " ";
});
cout << endl;
int arrIn_1[6] = { 1,2,4,4,4,5 };
vector<int> vecIn_1(arrIn_1, arrIn_1 + 6);
int arrSub_1[3] = { 1,3,4 };
vector<int> vecSub_1(arrSub_1, arrSub_1 + 3);
pair<vector<int>::iterator, vector<int>::iterator> itPair;
//短序列在前
itPair = mismatch(vecSub_1.begin(), vecSub_1.end(), vecIn_1.begin()); //mismatch
if (itPair.first != vecSub_1.end()) {
cout << "mismatch vecSub = " << *itPair.first << endl;
cout << "mismatch vecIn = " << *itPair.second << endl;
}
itPair = mismatch(vecSub_1.begin(), vecSub_1.end(), vecIn_1.begin() + 1, [](int &i,int &j) { //传入匿名函数
return i * 2 == j;
});
if (itPair.first != vecSub_1.end()) {
cout << "mismatch vecSub_1 = " << *itPair.first << endl;
cout << "mismatch vecIn_1 = " << *itPair.second << endl;
}
int arrSub_2[3] = { 1,2,4 };
vector<int> vecSub_2(arrSub_2, arrSub_2 + 3);
bool b = equal(vecSub_2.begin(), vecSub_2.end(), vecIn_1.begin()); //equal
if (b) {
cout << "vecSub_2 is equal to vecIn_1" << endl;
}
else {
cout << "vecSub_2 is not equal to vecIn_1" << endl;
}
int arrSub_3[3] = { 1,2,2 };
vector<int> vecSub_3(arrSub_3, arrSub_3 + 3);
b= equal(vecSub_3.begin(), vecSub_3.end(), vecIn_1.begin()+1, [](int &i, int &j) { //传入匿名函数
return i * 2 == j;
});
if (b) {
cout << "vecSub_3 is equal to vecIn_1" << endl;
}
else {
cout << "vecSub_3 is not equal to vecIn_1" << endl;
}
return 0;
}
// Desc: STL search() used in vector container and struct data
// Author: spring brother
// Data: 2018.3.24
// Copyright (C) 2018 spring brother
//----------------------------------------------------------------------------------------
/*
for_each(beg,end,unaryOp)
unaryOp的返回类型为void,当unaryOp传入的参数为引用类型时,可修改元素的值
mismatch(beg1,end1,beg2)
mismatch(beg1,end1,beg2,binaryPred)
返回一个迭代器的pair,指明两序列中第一个不匹配元素的位置。如果所有元素都匹配,
则返回的pair中的第一个迭代器为end1,第二个迭代器指向beg2中偏移量等于第一个序列长度的位置。
注意:短序列在前,长序列在后。
equal(beg1,end1,beg2)
equal(beg1,end1,beg2,binaryPred)
确定两个元素是否相等,如果输入序列中的每个元素都与从beg2开始的序列中对应元素相等,则返回true。
注意:短序列在前,长序列在后。
*/
#include "stdafx.h"
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
void print(int &i) {
cout << i <<" ";
}
void modifiedValue(int &i) {
i = i * 2;
}
int main()
{
int arrIn[6] = { 1,2,4,4,4,5 };
vector<int> vecIn(arrIn, arrIn + 6);
//传入仿函数
for_each(vecIn.begin(), vecIn.end(), print); //for_each
cout << endl;
for_each(vecIn.begin(), vecIn.end(), modifiedValue);
for_each(vecIn.begin(), vecIn.end(), print);
cout << endl;
//传入匿名函数
for_each(vecIn.begin(), vecIn.end(), [](int &i) {
cout << i << " ";
});
cout << endl;
for_each(vecIn.begin(), vecIn.end(), [](int &i) {
i = i * 2;
});
for_each(vecIn.begin(), vecIn.end(), [](int &i) {
cout << i << " ";
});
cout << endl;
int arrIn_1[6] = { 1,2,4,4,4,5 };
vector<int> vecIn_1(arrIn_1, arrIn_1 + 6);
int arrSub_1[3] = { 1,3,4 };
vector<int> vecSub_1(arrSub_1, arrSub_1 + 3);
pair<vector<int>::iterator, vector<int>::iterator> itPair;
//短序列在前
itPair = mismatch(vecSub_1.begin(), vecSub_1.end(), vecIn_1.begin()); //mismatch
if (itPair.first != vecSub_1.end()) {
cout << "mismatch vecSub = " << *itPair.first << endl;
cout << "mismatch vecIn = " << *itPair.second << endl;
}
itPair = mismatch(vecSub_1.begin(), vecSub_1.end(), vecIn_1.begin() + 1, [](int &i,int &j) { //传入匿名函数
return i * 2 == j;
});
if (itPair.first != vecSub_1.end()) {
cout << "mismatch vecSub_1 = " << *itPair.first << endl;
cout << "mismatch vecIn_1 = " << *itPair.second << endl;
}
int arrSub_2[3] = { 1,2,4 };
vector<int> vecSub_2(arrSub_2, arrSub_2 + 3);
bool b = equal(vecSub_2.begin(), vecSub_2.end(), vecIn_1.begin()); //equal
if (b) {
cout << "vecSub_2 is equal to vecIn_1" << endl;
}
else {
cout << "vecSub_2 is not equal to vecIn_1" << endl;
}
int arrSub_3[3] = { 1,2,2 };
vector<int> vecSub_3(arrSub_3, arrSub_3 + 3);
b= equal(vecSub_3.begin(), vecSub_3.end(), vecIn_1.begin()+1, [](int &i, int &j) { //传入匿名函数
return i * 2 == j;
});
if (b) {
cout << "vecSub_3 is equal to vecIn_1" << endl;
}
else {
cout << "vecSub_3 is not equal to vecIn_1" << endl;
}
return 0;
}