"C + +" Determines whether the element is in the vector, the vector to the weight, two vectors to seek the intersection, and set

Source: Internet
Author: User

#include <iostream>#include <vector>#include <algorithm> //sort function, orthogonal complement function#include <iterator> //finding iterators used by orthogonalusing namespacestd;//Print container vectorvoidPrint_vector (vector<int>v) {    if(V.size () >0) {cout<<"{";  for(intI=0;i<int(V.size ()); i++) {cout<<v[i]<<","; } cout<<"\b}"; }    Else{cout<<"{}"; }}//the de-weight of elements in a container vectorvector<int> Unique_element_in_vector (vector<int>v) {Vector<int>:: Iterator vector_iterator;    Sort (V.begin (), V.end ()); Vector_iterator=Unique (V.begin (), V.end ()); if(Vector_iterator! =V.end ())    {V.erase (Vector_iterator,v.end ()); }    returnv;}//two vectors to seek the intersection ofvector<int> Vectors_intersection (vector<int> v1,vector<int>v2) {Vector<int>v;       Sort (V1.begin (), V1.end ());       Sort (V2.begin (), V2.end ()); Set_intersection (V1.begin (), V1.end (), V2.begin (), V2.end (), Back_inserter (v));//ask for intersection    returnv;}//two vector and set of vectorsvector<int> Vectors_set_union (vector<int> v1,vector<int>v2) {Vector<int>v;       Sort (V1.begin (), V1.end ());       Sort (V2.begin (), V2.end ()); Set_union (V1.begin (), V1.end (), V2.begin (), V2.end (), Back_inserter (v));//ask for intersection    returnv;}//determine whether an element of a vector existsBOOLIs_element_in_vector (vector<int> V,intElement) {Vector<int>:: Iterator it; It=Find (V.begin (), v.end (), Element); if(it!=V.end ()) {        return true; }    Else{        return false; }}intmain () {vector<int>v1,v2,v; V1.push_back ( A); V1.push_back ( A); V1.push_back ( at); V2.push_back ( at); V2.push_back ( -); cout<<"is there a 1 element for v1? "<<is_element_in_vector (v1,1) <<Endl; cout<<"To v1 to the weight:"; V1=Unique_element_in_vector (v1);    Print_vector (v1); cout<<Endl; cout<<"To seek the intersection of V1 and V2:"; V=vectors_intersection (V1,V2);    Print_vector (v); cout<<Endl; cout<<"To seek the V1 and V2:"; V=vectors_set_union (V1,V2);    Print_vector (v); return 0;}

"C + +" Determines whether the element is in the vector, the vector to the weight, two vectors to seek the intersection, and set

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.