赞
踩
#include < algorithm >
【注意】如果找到了就直接返回,并不会继续找下去,所以判断没有找到的条件就是迭代器或者 指针指向了末尾元素的下一位就可以确定没有找到。
int main() { // using std::find with array and pointer: int myints[] = { 10, 20, 30, 40 }; int * p = find(myints, myints + 4, 30); if (p != myints + 4) { std::cout << "Element found in myints: " << *p << '\n'; } else { std::cout << "Element not found in myints\n"; } // using std::find with vector and iterator: std::vector<int> myvector(myints, myints + 4); std::vector<int>::iterator it; it = find(myvector.begin(), myvector.end(), 30); if (it != myvector.end()) { std::cout << "Element found in myvector: " << *it << '\n'; } else { std::cout << "Element not found in myvector\n"; } return 0; }
bool mypredicate(int i, int j) { return (i == j); } int main() { std::vector<int> v; // v: 10 20 30 40 50 60 70 80 90 for (int i = 1; i < 10; i++) v.push_back(i * 10); // using default comparison: int needle1[] = { 40,50,60,70 }; std::vector<int>::iterator it; it = search(v.begin(), v.end(), needle1, needle1 + 4); if (it != v.end()) { cout << (it - v.begin()) << '\n'; } else { cout << "needle1 not found\n"; } // using predicate comparison: int n[] = { 20,30,50 }; it =search(v.begin(), v.end(), n, n + 3, mypredicate); if (it != v.end()) { cout << (it - v.begin()) << '\n'; } else { cout << " not found\n"; } system("pause"); return 0; }
int main () { int first[] = {5,10,15,20,25}; int second[] = {50,40,30,20,10}; std::vector<int> v(10); std::sort (first,first+5); std::sort (second,second+5); std::merge (first,first+5,second,second+5,v.begin()); std::cout << "The resulting vector contains:"; for (std::vector<int>::iterator it=v.begin(); it!=v.end(); ++it) std::cout << ' ' << *it; std::cout << '\n'; return 0; }
int main() {
vector<int>v;
for (int i = 0; i < 10; ++i) {
v.push_back(i);
}
reverse(v.begin(), v.end());
for (int i = 0; i < 10; ++i) {
printf("%d ", v[i]);
}
cout << endl;
return 0;
}
int main() {
int a[10] = { 1,4,6,3,2,8,5,9,0 ,7};
vector<int>v(a,a+10);
sort(v.begin(), v.end());
for (auto&e : v) {
cout << ' ' << e;
}
cout << endl;
return 0;
}
默认是升序,但是可以自定以一个降序排序
//自定以函数
bool mcp(int x,int y) {
return x > y;
}
int main() {
int a[10] = { 1,4,6,3,2,8,5,9,0 ,7};
vector<int>v(a,a+10);
sort(v.begin(), v.end(),mcp);
for (auto&e : v) {
cout << ' ' << e;
}
cout << endl;
return 0;
}
int main() {
int myints[] = { 10,20,30,40,50,60,70 };
std::vector<int> myvector(7);
std::vector<int>::iterator it;
std::copy(myints, myints + 7, myvector.begin());
std::cout << "myvector contains:";
for (it = myvector.begin(); it != myvector.end(); ++it)
std::cout << ' ' << *it;
std::cout << '\n';
return 0;
}
int main() {
int myints[] = { 10,20,30,40,50,60,70,20,20 ,30};
int* end = myints + sizeof(myints) / sizeof(int);
end = remove(myints, end, 20);
std::cout << "myvector contains:";
for (int* i = myints; i < end; ++i) {
cout << ' ' << *i;
}
std::cout << '\n';
return 0;
}
int main() {
int myints[] = { 10,20,30,40,50,60,70,20,20 ,30};
vector<int>v(myints,myints+10);
replace(v.begin(), v.end(),20,200);
std::cout << "v contains:";
for (auto&e : v) {
cout << ' ' << e;
}
std::cout << '\n';
return 0;
}
equal: 如果两个序列在标志范围内元素都相等,返回true。重载版本使用输入的操作符代替默认的等于操作符。
includes: 判断第一个指定范围内的所有元素是否都被第二个范围包含,使用底层元素的<操作符,成功返回true。重载版本使用用户输入的函数。
max: 返回两个元素中较大一个。重载版本使用自定义比较操作。
min: 返回两个元素中较小一个。重载版本使用自定义比较操作。
以上这些都是经常用到的函数,如果想浏览更多函数
参考 -------》 这篇博客 《-------点这里
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。