STL Mutating Algorithms小结

2024-05-27 06:38
文章标签 小结 stl algorithms mutating

本文主要是介绍STL Mutating Algorithms小结,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Non-mutating Algorithms相比,变易算法能修改容器元素数据,可进行序列数据的复制、交换、替换、填充、移除、旋转、随机抖动、分割。还是参考叶至军的那本书以及网站Cplusplus.com

copy

 

元素复制。该函数用于容器间元素拷贝,将迭代器区间[first, last)的元素复制到由复制目标迭代器result给定的区间[result, result + (last - first))

template <class InputIterator, class OutputIterator>OutputIterator copy (InputIterator first, InputIterator last, OutputIterator result);Copies the elements in the range [first,last) into the range beginning at result.
The function returns an iterator to the end of the destination range (which points to the element following the last element copied).template<class InputIterator, class OutputIterator>OutputIterator copy (InputIterator first, InputIterator last, OutputIterator result)
{while (first!=last) {*result = *first;++result; ++first;}return result;
}#include<iostream>
#include<algorithm>
#include<list>
#include<vector>
using namespace std;
void print(int x)
{cout<<x<<" ";
}
int main()
{vector<int> v;v.push_back(2);v.push_back(4);v.push_back(3);list<int> l(3);copy(v.begin(), v.end(), l.begin());for_each(l.begin(), l.end(), print);//2 4 3return 0;
}


 

copy_backward

反向复制。将一个迭代器区间的内容复制到另一迭代器区间,与copy()相似,不同的是复制过程从最后的元素开始。

template <class BidirectionalIterator1, class BidirectionalIterator2>BidirectionalIterator2 copy_backward (BidirectionalIterator1 first,BidirectionalIterator1 last,BidirectionalIterator2 result);
Copies the elements in the range [first,last) starting from the end into the range terminating at result.
The function returns an iterator to the first element in the destination range.template<class BidirectionalIterator1, class BidirectionalIterator2>BidirectionalIterator2 copy_backward ( BidirectionalIterator1 first,BidirectionalIterator1 last,BidirectionalIterator2 result )
{while (last!=first) *(--result) = *(--last);return result;
}#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
void print(int x)
{cout<<x<<" ";
}
int main()
{vector<int> v(4);for(int i=0;i<v.size();i++){v[i]=i+1;}vector<int> v2(8);copy_backward(v.begin(), v.end(), v2.end());for_each(v2.begin(), v2.end(), print);//0 0 0 0 1 2 3 4return 0;
}


swap

该函数的功能是实现两个元素的交换。虽然大多数容器内部都提供了swap函数,这里提供了更一般的迭代器形式。

C++98中,函数原型为:
template <class T> void swap (T& a, T& b);
C++11中,函数原型为:
template <class T> void swap (T& a, T& b)noexcept 
(is_nothrow_move_constructible<T>::value && is_nothrow_move_assignable<T>::value);
template <class T, size_t N> void swap(T (&a)[N], T (&b)[N])noexcept (noexcept(swap(*a,*b)));template <class T> void swap (T& a, T& b)
{T c(std::move(a)); a=std::move(b); b=std::move(c);
}
template <class T, size_t N> void swap (T (&a)[N], T (&b)[N])
{for (size_t i = 0; i<N; ++i) swap (a[i],b[i]);
}
//交换a,b的值
#include <iostream>     // std::cout
#include <algorithm>    // std::swap
#include <vector>       // std::vector
int main () {int x=10, y=20;                              // x:10 y:20std::swap(x,y);                              // x:20 y:10std::vector<int> foo (4,x), bar (6,y);       // foo:4x20 bar:6x10std::swap(foo,bar);                          // foo:6x10 bar:4x20std::cout << "foo contains:";for (std::vector<int>::iterator it=foo.begin(); it!=foo.end(); ++it)std::cout << ' ' << *it;return 0;
}

iter_swap

 迭代器交换。iter_swap函数是swap函数的迭代器形式,使交换算法更易用于一般的容器。

template <class ForwardIterator1, class ForwardIterator2>void iter_swap (ForwardIterator1 a, ForwardIterator2 b);
Swaps the elements pointed to by a and b.
The function calls swap (unqualified) to exchange the elements.template <class ForwardIterator1, class ForwardIterator2>void iter_swap (ForwardIterator1 a, ForwardIterator2 b)
{swap (*a, *b);
}#include <iostream>     // std::cout
#include <algorithm>    // std::iter_swap
#include <vector>       // std::vector
int main () {int myints[]={10,20,30,40,50 };              //   myints:  10  20  30  40  50std::vector<int> myvector (4,99);            // myvector:  99  99  99  99std::iter_swap(myints,myvector.begin());     //   myints: [99] 20  30  40  50// myvector: [10] 99  99  99std::iter_swap(myints+3,myvector.begin()+2); //   myints:  99  20  30 [99] 50// myvector:  10  99 [40] 99std::cout << "myvector contains:";for (std::vector<int>::iterator it=myvector.begin(); it!=myvector.end(); ++it)std::cout << ' ' << *it;return 0;
}

swap_ranges

区间元素交换。

函数原型如下:

template<class ForwardIterator1, class ForwardIterator2>ForwardIterator2 swap_ranges (ForwardIterator1 first1, ForwardIterator1 last1,ForwardIterator2 first2)Exchanges the values of each of the elements in the range [first1,last1) with those of their respective elements in the range beginning at first2.
The function calls swap (unqualified) to exchange the elements.template<class ForwardIterator1, class ForwardIterator2>ForwardIterator2 swap_ranges (ForwardIterator1 first1, ForwardIterator1 last1,ForwardIterator2 first2)
{while (first1!=last1) {swap (*first1, *first2);++first1; ++first2;}return first2;
}#include <iostream>     // std::cout
#include <algorithm>    // std::swap_ranges
#include <vector>       // std::vectorint main () {std::vector<int> foo (5,10);        // foo: 10 10 10 10 10std::vector<int> bar (6,33);        // bar: 33 33 33 33 33 33std::swap_ranges(foo.begin()+1, foo.end()-1, bar.begin());// print out results of swap:std::cout << "foo contains:";for (std::vector<int>::iterator it=foo.begin(); it!=foo.end(); ++it)std::cout << ' ' << *it;std::cout<<std::endl;std::cout << "bar contains:";for (std::vector<int>::iterator it=bar.begin(); it!=bar.end(); ++it)std::cout << ' ' << *it;std::cout <<std::endl;return 0;
}

transform

元素变换。该函数用于容器元素的变换操作。有两个使用原型,分别用于一元函数对象和二元函数操作。

template <class InputIterator, class OutputIterator, class UnaryOperation>OutputIterator transform (InputIterator first1, InputIterator last1,OutputIterator result, UnaryOperation op)template <class InputIterator1, class InputIterator2,class OutputIterator, class BinaryOperation>OutputIterator transform (InputIterator1 first1, InputIterator1 last1,InputIterator2 first2, OutputIterator result,BinaryOperation binary_op)Applies an operation sequentially to the elements of one (1) or two (2) ranges and stores the result in the range that begins at result.template <class InputIterator, class OutputIterator, class UnaryOperator>OutputIterator transform (InputIterator first1, InputIterator last1,OutputIterator result, UnaryOperator op)
{while (first1 != last1) {*result = op(*first1);  // or: *result=binary_op(*first1,*first2++);++result; ++first1;}return result;
}#include <iostream>     // std::cout
#include <algorithm>    // std::transform
#include <vector>       // std::vector
#include <functional>   // std::plusint op_increase (int i) { return ++i; }int main () {std::vector<int> foo;std::vector<int> bar;// set some values:for (int i=1; i<6; i++)foo.push_back (i*10);                         // foo: 10 20 30 40 50bar.resize(foo.size());                         // allocate spacestd::transform (foo.begin(), foo.end(), bar.begin(), op_increase);// bar: 11 21 31 41 51// std::plus adds together its two arguments:std::transform (foo.begin(), foo.end(), bar.begin(), foo.begin(), std::plus<int>());// foo: 21 41 61 81 101std::cout << "foo contains:";for (std::vector<int>::iterator it=foo.begin(); it!=foo.end(); ++it)std::cout << ' ' << *it;std::cout << '\n';return 0;
}

replace

替换。

template <class ForwardIterator, class T>void replace (ForwardIterator first, ForwardIterator last,const T& old_value, const T& new_value)template <class ForwardIterator, class T>void replace (ForwardIterator first, ForwardIterator last,const T& old_value, const T& new_value)
{while (first!=last) {if (*first == old_value) *first=new_value;++first;}
}#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
void print(int x) {cout << x << " ";
}
int main(void) {vector<int> v;v.push_back(3);v.push_back(5);v.push_back(8);v.push_back(5);replace(v.begin(), v.end(), 5, 6);for_each(v.begin(), v.end(), print);//3 6 8 6return 0;
}


 

replace_if

条件替换。该函数是replace函数的谓词判断版本,将满足谓词条件的元素替换为新值。

 

template <class ForwardIterator, class UnaryPredicate, class T>void replace_if (ForwardIterator first, ForwardIterator last,UnaryPredicate pred, const T& new_value );#include <iostream>     // std::cout
#include <algorithm>    // std::replace_if
#include <vector>       // std::vectorbool IsOdd (int i) { return ((i%2)==1); }int main () {std::vector<int> myvector;// set some values:for (int i=1; i<10; i++) myvector.push_back(i);               // 1 2 3 4 5 6 7 8 9std::replace_if (myvector.begin(), myvector.end(), IsOdd, 0); // 0 2 0 4 0 6 0 8 0std::cout << "myvector contains:";for (std::vector<int>::iterator it=myvector.begin(); it!=myvector.end(); ++it)std::cout << ' ' << *it;std::cout << '\n';return 0;
}

replace_copy

替换和复制。该函数先进行元素替换,再将元素复制到新容器。

template <class BidirectionalIterator, class OutputIterator>OutputIterator reverse_copy (BidirectionalIterator first,BidirectionalIterator last, OutputIterator result);//将容器v中值为5的元素的值替换为6,并复制到容器v2。
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
void print(int x) {cout << x << " ";
}
int main(void) {vector<int> v;v.push_back(3);v.push_back(5);v.push_back(8);vector<int> v2(v.size());replace_copy(v.begin(), v.end(), v2.begin(), 5, 6);for_each(v2.begin(), v2.end(), print);return 0;
}


 

replace_copy_if

条件替换和复制。该函数是replace_copy的一元谓词判断版本,用法相似。

template <class InputIterator, class OutputIterator, class UnaryPredicate, class T>OutputIterator replace_copy_if (InputIterator first, InputIterator last,OutputIterator result, UnaryPredicate pred,const T& new_value)#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
void print(int x) {cout << x << " ";
}
int odd(int x) {return !(x % 2);
}
int main(void) {vector<int> v;v.push_back(3);v.push_back(5);v.push_back(8);vector<int> v2(v.size());replace_copy_if(v.begin(), v.end(), v2.begin(), odd, 2);for_each(v2.begin(), v2.end(), print);return 0;
}

fill

填充。该函数将同一个值填充到迭代器区间内。

template <class ForwardIterator, class T>void fill (ForwardIterator first, ForwardIterator last, const T& val)#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
void print(int x) {cout << x << " ";
}
int main(void) {vector<int> v(5);fill(v.begin(), v.end(), 9);for_each(v.begin(), v.end(), print);return 0;
}

fill_n

n次填充。与fill函数相似,但可指定填充的元素的个数。

template <class OutputIterator, class Size, class T>void fill_n (OutputIterator first, Size n, const T& val)#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
void print(int x)
{cout<<x<<" ";
}
int main()
{vector<int> v(4);for(int i=0;i<v.size();i++)v[i]=i+1;fill_n(v.begin()+1,2,10);for_each(v.begin(),v.end(),print);//1 10 10 4return 0;
}

generate

随机生成元素。该函数用于随机生成函数,将迭代器区间内的元素填充为生成的元素。


template <class ForwardIterator, class Generator>void generate (ForwardIterator first, ForwardIterator last, Generator gen)
//生成公比为3的等比数列
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
void print(int x) {cout << x << " ";
}
int next(int x) {return 2;
}
class Seq {
public:int a;Seq() {a = 1;}inline int operator()() {a *= 3;return a;}
};
int main(void) {vector<int> v(5);Seq seq;generate(v.begin(), v.end(), seq);for_each(v.begin(), v.end(), print);return 0;
}

generate_n

随机生成n个元素。该函数与generate相似,只是限定了填入容器的数值个数。函数原型:

template <class OutputIterator, class Size, class Generator>void generate_n (OutputIterator first, Size n, Generator gen)#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
void print(int x)
{cout<<x<<" ";
}int main()
{vector<int> v(6);generate_n(v.begin(),3, rand);for_each(v.begin(),v.end(),print);return 0;
}

remove

移除。该函数用于将容器中等于某个给定值的元素全部移除掉,并返回表示容器结束位置的迭代器。函数原型:

template <class ForwardIterator, class T>ForwardIterator remove (ForwardIterator first, ForwardIterator last, const T& val)template <class ForwardIterator, class T>ForwardIterator remove (ForwardIterator first, ForwardIterator last, const T& val)
{ForwardIterator result = first;while (first!=last) {if (!(*first == val)) {*result = move(*first);++result;}++first;}return result;
}
//移除数组中的元素20
#include <iostream>     // std::cout
#include <algorithm>    // std::removeint main () {int myints[] = {10,20,30,30,20,10,10,20};      // 10 20 30 30 20 10 10 20// bounds of range:int* pbegin = myints;                          // ^int* pend = myints+sizeof(myints)/sizeof(int); // ^                       ^pend = std::remove (pbegin, pend, 20);         // 10 30 30 10 10 ?  ?  ?// ^              ^std::cout << "range contains:";for (int* p=pbegin; p!=pend; ++p)std::cout << ' ' << *p;std::cout << '\n';return 0;
}

 

remove_if

条件移除。该函数是remove函数的一个带谓词判断的版本,将满足一元谓词判断条件的元素移除掉。

template <class ForwardIterator, class UnaryPredicate>ForwardIterator remove_if (ForwardIterator first, ForwardIterator last,UnaryPredicate pred)
//将容器v中值为偶数的元素移除掉
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int isOdd(int x) {return !(x % 2);
}
void print(int x) {cout << x << " ";
}
int main(void) {vector<int> v;v.push_back(3);v.push_back(7);v.push_back(8);vector<int>::iterator result =  remove_if(v.begin(), v.end(), isOdd);for_each(v.begin(), result, print);return 0;
}

remove_copy

移除复制。该函数先进行元素移除,再将元素复制到新容器,实质上是一个条件复制。

 

template <class InputIterator, class OutputIterator, class T>OutputIterator remove_copy (InputIterator first, InputIterator last,OutputIterator result, const T& val)
Copies the elements in the range [first,last) to the range beginning at result, except those elements that compare equal to val.
template <class InputIterator, class OutputIterator, class T>OutputIterator remove_copy (InputIterator first, InputIterator last,OutputIterator result, const T& val)
{while (first!=last) {if (!(*first == val)) {*result = *first;++result;}++first;}return result;
}#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int isOdd(int x) {return !(x % 2);
}
void print(int x) {cout << x << " ";
}
int main(void) {vector<int> v;v.push_back(3);v.push_back(7);v.push_back(8);vector<int>::iterator result =  remove_if(v.begin(), v.end(), isOdd);for_each(v.begin(), result, print);return 0;
}


remove_copy_if

条件移除复制。该函数是remove_copy的一个带谓词判断版本,将不满足一元谓词判断条件的元素复制到新容器。

 

template <class InputIterator, class OutputIterator, class UnaryPredicate>OutputIterator remove_copy_if (InputIterator first, InputIterator last,OutputIterator result, UnaryPredicate pred)#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int isOdd(int x) {return !(x % 2);
}
void print(int x) {cout << x << " ";
}
int main(void) {vector<int> v;v.push_back(3);v.push_back(7);v.push_back(8);vector<int> v2(5);remove_copy_if(v.begin(), v.end(), v2.begin(), isOdd);for_each(v2.begin(), v2.end(), print);return 0;
}


 unique

剔除连续重复元素。该函数用于剔除容器中连续重复的元素,只保留一个。有两个使用原型,可以使连续重复的原型,或不连续但满足二元谓词判断条件的元素。

template <class ForwardIterator>ForwardIterator unique (ForwardIterator first, ForwardIterator last)template <class ForwardIterator, class BinaryPredicate>ForwardIterator unique (ForwardIterator first, ForwardIterator last,BinaryPredicate pred)#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
void print(int x) {cout << x << " ";
}
int main(void) {vector<int> v;v.push_back(2);v.push_back(2);v.push_back(3);v.push_back(2);v.push_back(5);vector<int>::iterator result = unique(v.begin(), v.end());for_each(v.begin(), result, print);//2 3 2 5return 0;
}

 

unique_copy

不连续重复元素复制。该函数用于复制不连续重复的元素。同样有两个使用原型。

template <class InputIterator, class OutputIterator>OutputIterator unique_copy (InputIterator first, InputIterator last,OutputIterator result)template <class InputIterator, class OutputIterator, class BinaryPredicate>OutputIterator unique_copy (InputIterator first, InputIterator last,OutputIterator result, BinaryPredicate pred)#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
void print(int x) {cout << x << " ";
}
int main(void) {vector<int> v;v.push_back(2);v.push_back(3);v.push_back(3);v.push_back(5);v.push_back(3);vector<int> v2(4);unique_copy(v.begin(), v.end(), v2.begin());for_each(v2.begin(), v2.end(), print);//2 3 5 3return 0;
}

reverse

元素反向。该函数用于容器元素的反向排列。

template <class BidirectionalIterator>void reverse (BidirectionalIterator first, BidirectionalIterator last)#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
void print(int x) {cout << x << " ";
}
int main(void) {vector<int> v;v.push_back(2);v.push_back(3);v.push_back(5);reverse(v.begin(), v.end());for_each(v.begin(), v.end(), print);return 0;
}

 

reverse_copy

反向复制。该函数用于反向复制容器元素。

template <class BidirectionalIterator, class OutputIterator>OutputIterator reverse_copy (BidirectionalIterator first,BidirectionalIterator last, OutputIterator result)//将容器v中的元素反向复制到新容器v2中
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
void print(int x) {cout << x << " ";
}
int main(void) {vector<int> v;v.push_back(2);v.push_back(3);v.push_back(5);vector<int> v2(v.size());reverse_copy(v.begin(), v.end(), v2.begin());for_each(v2.begin(), v2.end(), print);return 0;
}

rotate

该函数用于旋转某个迭代器区间的元素。假设区间元素为{a0, a1, ..., ak, ..., an}akmiddle位置,则旋转复制后,新区间元素为{ak, ak+1, ..., an, a0, a1, ..., ak-1}

函数原型:

 

template <class ForwardIterator>void rotate (ForwardIterator first, ForwardIterator middle,ForwardIterator last)//将容器v以值为7的元素为支点旋转#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
void print(int x) {cout << x << " ";
}
int main(void) {vector<int> v;for(int i = 0; i < 10; i++) {v.push_back(i + 1);}rotate(v.begin(), find(v.begin(), v.end(), 7), v.end());for_each(v.begin(), v.end(), print);return 0;
}

rotate_copy

旋转复制。该函数通过复制的方法实现旋转,比rotate函数更简单高效,但需要占用较多的内存空间。

template <class ForwardIterator, class OutputIterator>OutputIterator rotate_copy (ForwardIterator first, ForwardIterator middle,ForwardIterator last, OutputIterator result)//将以容器v中值为7的元素为支点旋转,将结果存储在容器v2中
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
void print(int x) {cout << x << " ";
}
int main(void) {vector<int> v;for(int i = 0; i < 10; i++) {v.push_back(i + 1);}vector<int> v2(v.size());rotate_copy(v.begin(), find(v.begin(), v.end(), 7), v.end(), v2.begin());for_each(v2.begin(), v2.end(), print);return 0;
}

 

random_shuffle

随机抖动。该函数用于对容器元素进行随机的排列,有两个使用原型,使用C标准的伪随机函数rand或自定义的随机数发生器函数对象。

 

template <class RandomAccessIterator>void random_shuffle (RandomAccessIterator first, RandomAccessIterator last)template <class RandomAccessIterator, class RandomNumberGenerator>void random_shuffle (RandomAccessIterator first, RandomAccessIterator last,RandomNumberGenerator& gen)#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
void print(int x) {cout << x << " ";
}
int main(void) {vector<int> v;for(int i = 0; i < 10; i++) {v.push_back(i);}random_shuffle(v.begin(), v.end());for_each(v.begin(), v.end(), print);cout << endl;random_shuffle(v.begin(), v.end());for_each(v.begin(), v.end(), print);return 0;
}

random_sample

随机采样。SGI C++可能不支持。

 

#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
void print(int x)
{cout<<x<<" ";
}
int main()
{vector<int> v(10);for(int i=0;i<v.size();i++){v[i]=i%8;}int n=6;int iArray[n];random_sample(v.begin(),v.end(),iArray,iArray+n);cout<<"采样元素为:";for_each(iArray,iArray+n,print);cout<<endl;return 0;
}

partition

容器分割。该函数用于重新分割排列容器的元素,按照医院谓词判断条件,分割成满足和不满足条件的两部分,返回的迭代器指向首个不满足条件的元素。

 

template <class BidirectionalIterator, class UnaryPredicate>BidirectionalIterator partition (BidirectionalIterator first,BidirectionalIterator last, UnaryPredicate pred)
//将容器v中的元素分为小于5和大于等于5的两部分,并分别将元素打印出来#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
void print(int x) {cout << x << " ";
}
int less5(int x) {if(x < 5) {return 1;} else {return 0;}
}
int main(void) {vector<int> v;for(int i = 10; i > 0; i--) {v.push_back(i);}vector<int>::iterator iv = partition(v.begin(), v.end(), less5);cout << "less than 5: ";for_each(v.begin(), iv, print);cout << endl;cout << "no less than 5: ";for_each(iv, v.end(), print);return 0;
}

stable_partition

容器稳定分割。该函数与parition类似,重新分割排列容器的元素,但可以保持原有元素的先后顺序。

 

template <class BidirectionalIterator, class UnaryPredicate>BidirectionalIterator stable_partition (BidirectionalIterator first,BidirectionalIterator last,UnaryPredicate pred)Rearranges the elements in the range [first,last), in such a way that all the elements for which pred returns true precede all those for which it returns false, and, unlike function partition, the relative order of elements within each group is preserved.
//将容器v中的元素分为小于5和大于等于5的两部分,但保持元素的稳定性
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
void print(int x) {cout << x << " ";
}
int less5(int x) {if(x < 5) {return 1;} else {return 0;}
}
int main(void) {vector<int> v;for(int i = 10; i > 0; i--) {v.push_back(i);}vector<int>::iterator iv = stable_partition(v.begin(), v.end(), less5);cout << "less than 5: ";for_each(v.begin(), iv, print);cout << endl;cout << "no less than 5: ";for_each(iv, v.end(), print);return 0;
}

 

这篇关于STL Mutating Algorithms小结的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1006724

相关文章

C++标准模板库STL介绍

STL的六大组成部分 STL(Standard Template Library)是 C++ 标准库中的一个重要组成部分,提供了丰富的通用数据结构和算法,使得 C++ 编程变得更加高效和方便。STL 包括了 6 大类组件,分别是算法(Algorithm)、容器(Container)、空间分配器(Allocator)、迭代器(Iterator)、函数对象(Functor)、适配器(Adapter)

STL迭代器的基础应用

STL迭代器的应用 迭代器的定义方法: 类型作用定义方式正向迭代器正序遍历STL容器容器类名::iterator 迭代器名常量正向迭代器以只读方式正序遍历STL容器容器类名::const_iterator 迭代器名反向迭代器逆序遍历STL容器容器类名::reverse_iterator 迭代器名常量反向迭代器以只读方式逆序遍历STL容器容器类名::const_reverse_iterato

泛型和Integer小结

泛型在Java.util里面找:也可对其他你使用的函数进行查阅 如下:只要函数后面跟有尖括号<>,你都可以拿过来使用泛型     注意:用到集合时尽量使用泛型       int与Integer   integer这个不是关键字,是java的一个类。也就是int的包装类。int是基本数据类型,integer是引用类型,包含很多属性和方法,而int只是一个值,没有其他的任何

如何使用STL中的模板类

在C++中,标准模板库(STL)提供了大量的模板类,这些类可以处理各种类型的数据,从而极大地提高了代码的复用性和灵活性。要使用STL中的模板类,你需要遵循一些基本的步骤和约定。 以下是一些使用STL模板类的基本步骤: 包含头文件: 首先,你需要包含相应的STL头文件,以便能够使用其中的模板类。例如,要使用std::vector,你需要包含<vector>头文件。 cpp复制代码 #incl

我的OS X系统使用小结

最近硬盘出现了故障,为了恢复确认问题,我的Windows电脑就一直运行坏道检测程序,暂时将工作迁移到我的MacBook Pro(后面简称为MBP)上进行。这里简单总结下自己是如何使用MBP进行做的。 原本计划是用我的小米游戏笔记本,但是不知为何,即便是非游戏模式,风扇偶尔也会狂转,产生很大的噪音。相反,MBP非常安静,就是有点烫手。 我的MBP是17年款,13寸屏幕,蝶式键盘。为了不烫手

C++STL 初阶(5)vector的简易实现(上)

不同于string只实现一个最简单的版本,vector在此处我们要实现的是模版类,类模版的声明和定义分离非常不方便(会在链接时报错),所以我们都只在一个vector.h下去实现声明和定义。后续我们提及到的库里面实现的vector也是只有.h,没有.cpp      不过库中会将短的函数放在类里,如size、begin等(直接作为inline函数),大的如insert_aux就会放在

python装饰器小结

按照我自己的理解,python装饰器的作用就是让一些重复性的“操作”(代码)只出现一次就好,这样整个代码看起来会更清晰整洁,主要也减少了无谓的赋值粘贴。 例如,当前有两个方法get请求和post请求,在执行两个方法前,都要先检查一下请求的url是否符合要求,一般会这样写: # encoding=utf8import requestsclass Test():def __init__(self)

STL vector容器自己实现

本文参考了侯捷的 《STL 源码分析》一书,出于兴趣,自行实现了简单的 vector 容器。 之后会陆续上传 list, deque 等容器的代码,若有错误,欢迎留言指出。 vector 容易实现的几点注意事项: 1. 由于vector 是动态数组。 出于效率的考虑,在往vector 中加入元素时,内存的扩展遵循的规则是:    1> 如果当前可用内存不够,开 2倍大的内存,将原来

STL源码分析之大顶堆

关于大顶堆和小顶堆这里就不再介绍了,这里通过STL再次回顾一下。 heap为了适应容器大小的不断变化,底层调用vector实现 关于heap的算法(这里是大顶堆)   push_heap()算法 为了满足完全二叉树的条件,新加入的元素一定是放在最下一层作为叶节点,并填补在由左至右的第一个空格,即插在vector的end()处     我们通过上溯,将新节点与其父节点进

STL源码分析之vector容器

vector与array非常相似,两者唯一差别在于空间运用的灵活性          array是静态空间,一旦配置了就不能改变;若要改变使用的空间大小,一切琐碎由客户端自己来处理:先配置新空间,然后将元素从旧址一一搬到新址去,在退还原来的空间          vector则是动态空间,随着元素的加入,它的内部机制会自行扩充空间以容纳新的元素   以下为sgi vector源码: