C++/Vector/vector find

Материал из C\C++ эксперт
Перейти к: навигация, поиск

Binary search a vector

<source lang="cpp">

  1. include <algorithm>
  2. include <vector>
  3. include <iostream>

using namespace std; void print(int elem){

 cout << elem << " ";

} int main(int argc, char** argv) {

 vector<int> v1, v2, vectorMerged;
 v1.push_back(1);
 v1.push_back(2);
 v1.push_back(3);
 v2.push_back(2);
 v2.push_back(3);
 v2.push_back(4);
 sort(v1.begin(), v1.end());
 sort(v2.begin(), v2.end());
 vectorMerged.resize(v1.size() + v2.size());
 merge(v1.begin(), v1.end(), v2.begin(),v2.end(), vectorMerged.begin());
 if (binary_search(vectorMerged.begin(), vectorMerged.end(), 3)) {
     cout << "That number is in the vector.\n";
 } else {
     cout << "That number is not in the vector\n";
 }
 return (0);

}


 </source>


Demonstrating the generic find algorithm with a vector

<source lang="cpp">

  1. include <iostream>
  2. include <cassert>
  3. include <vector>
  4. include <algorithm> // For find

using namespace std;

int main() {

 char x[5] = {"a", "b", "c", "d", "f"};
 vector<char> vector1(&x[0], &x[5]);
 // Search for the first occurrence
 vector<char>::iterator where = find(vector1.begin(), vector1.end(), "b");
 cout << *where  << endl;
 return 0;

} /* b

*/        
   
 </source>


Find the first of two values

<source lang="cpp">

  1. include <algorithm>
  2. include <iostream>
  3. include <vector>

using namespace std; int main(int argc, char** argv) {

 int elems[] = {5, 6, 9, 8, 8, 3};
 vector<int> myVector(elems, elems + 6); 
 vector<int>::const_iterator it, it2;  
 // Find the first of two values
 int targets[] = {8, 9};
 it = find_first_of(myVector.begin(), myVector.end(), targets,targets + 2);
 if (it != myVector.end()) {
   cout << "Found one of 8 or 9: " << *it << endl;
 }
 return (0);

}


 </source>


Find the first pair of matching consecutive elements

<source lang="cpp">

  1. include <algorithm>
  2. include <iostream>
  3. include <vector>

using namespace std; int main(int argc, char** argv) {

 int elems[] = {5, 6, 9, 8, 8, 3};
 vector<int> myVector(elems, elems + 6); 
 vector<int>::const_iterator it, it2;  
 // Find the first pair of matching consecutive elements
 it = adjacent_find(myVector.begin(), myVector.end());
 if (it != myVector.end()) {
   cout << "Found two consecutive equal elements of value " << *it << endl;
 }
 return (0);

}


 </source>


Find the first subsequence

<source lang="cpp">

  1. include <algorithm>
  2. include <iostream>
  3. include <vector>

using namespace std; int main(int argc, char** argv) {

 int elems[] = {5, 6, 9, 8, 8, 3};
 vector<int> myVector(elems, elems + 6); 
 vector<int>::const_iterator it, it2;  
 // Find the first subsequence
 int sub[] = {8, 3};
 it = search(myVector.begin(), myVector.end(), sub, sub + 2);
 if (it != myVector.end()) {
   cout << "Found subsequence 8, 3 at position " << it - myVector.begin()
  << endl;
 }
 return (0);

}


 </source>


Find the first subsequence of two consecutive 8s

<source lang="cpp">

  1. include <algorithm>
  2. include <iostream>
  3. include <vector>

using namespace std; int main(int argc, char** argv) {

 int elems[] = {5, 6, 9, 8, 8, 3};
 vector<int> myVector(elems, elems + 6); 
 vector<int>::const_iterator it, it2;  
 it = search_n(myVector.begin(), myVector.end(), 2, 8);
 if (it != myVector.end()) {
   cout << "Found two consecutive 8s starting at position "
  << it - myVector.begin() << endl;
 }
 return (0);

}


 </source>


Find the last subsequence (which should be the same as the first)

<source lang="cpp">

  1. include <algorithm>
  2. include <iostream>
  3. include <vector>

using namespace std; int main(int argc, char** argv) {

 int elems[] = {5, 6, 9, 8, 8, 3};
 vector<int> myVector(elems, elems + 6); 
 vector<int>::const_iterator it, it2;  
 int sub[] = {8, 3};
 
 it2 = find_end(myVector.begin(), myVector.end(), sub, sub + 2);
 if (it != it2) {
   cout << "Error: search and find_end found different subsequences "
  << " even though there is only one match.\n";
 }
 return (0);

}


 </source>


Find the max value in the vector

<source lang="cpp">

  1. include <algorithm>
  2. include <iostream>
  3. include <vector>

using namespace std; int main(int argc, char** argv) {

 int elems[] = {5, 6, 9, 8, 8, 3};
 vector<int> myVector(elems, elems + 6); 
 vector<int>::const_iterator it, it2;  
 // Find the min and max elements in the vector
 it2 = max_element(myVector.begin(), myVector.end());
 cout << " the max is " << *it2 << endl;
 return (0);

}


 </source>


Find the min value in the vector

<source lang="cpp">

  1. include <algorithm>
  2. include <iostream>
  3. include <vector>

using namespace std; int main(int argc, char** argv) {

 int elems[] = {5, 6, 9, 8, 8, 3};
 vector<int> myVector(elems, elems + 6); 
 vector<int>::const_iterator it, it2;  
 // Find the min and max elements in the vector
 it = min_element(myVector.begin(), myVector.end());
 cout << "The min is " << *it << endl;
 return (0);

}


 </source>


Locate maximum element in a vector

<source lang="cpp">

  1. include <iostream>

using std::cout; using std::endl;

  1. include <algorithm>
  2. include <numeric>
  3. include <vector>
  4. include <iterator>

int main() {

  std::ostream_iterator< int > output( cout, " " );
  int a2[ 10 ] = { 100, 2, 8, 1, 50, 3, 8, 8, 9, 10 };
  std::vector< int > v2( a2, a2 + 10 ); // copy of a2
  cout << "\n\nVector v2 contains: ";
  std::copy( v2.begin(), v2.end(), output );
  // locate maximum element in v2
  cout << "\nMaximum element in Vector v2 is: "
     << *( std::max_element( v2.begin(), v2.end() ) );
  cout << endl;
  return 0;

} /*

Vector v2 contains: 100 2 8 1 50 3 8 8 9 10 Maximum element in Vector v2 is: 100

*/        
   
 </source>


Locate minimum element in a vector

<source lang="cpp">


  1. include <iostream>

using std::cout; using std::endl;

  1. include <algorithm>
  2. include <numeric>
  3. include <vector>
  4. include <iterator>

int main() {

  std::ostream_iterator< int > output( cout, " " );
  int a2[ 10 ] = { 100, 2, 8, 1, 50, 3, 8, 8, 9, 10 };
  std::vector< int > v2( a2, a2 + 10 ); // copy of a2
  cout << "\n\nVector v2 contains: ";
  std::copy( v2.begin(), v2.end(), output );
  // locate minimum element in v2
  cout << "\n\nMinimum element in Vector v2 is: "
     << *( std::min_element( v2.begin(), v2.end() ) );
  cout << endl;
  return 0;

} /*

Vector v2 contains: 100 2 8 1 50 3 8 8 9 10 Minimum element in Vector v2 is: 1

*/        
   
 </source>


Returns the positions of all values within a range

<source lang="cpp">

  1. include <iostream>
  2. include <vector>

using namespace std; vector<int> find_all_between(vector<double> v, double low, double high) {

  vector<int> pos;
  for (int i = 0; i < v.size(); i++) 
  { 
     if (low <= v[i] && v[i] <= high) 
        pos.push_back(i); 
  } 
  return pos; 

} int main() {

  vector<double> salaries(5); 
  salaries[0] = 3.0; 
  salaries[1] = 6.0; 
  salaries[2] = 4.0; 
  salaries[3] = 7.0; 
  salaries[4] = 5.0; 
  
  vector<int> matches = find_all_between(salaries, 4.0, 6.0); 
  
  for (int j = 0; j < matches.size(); j++) 
     cout << salaries[matches[j]] << "\n"; 
  return 0; 

}


 </source>


Search the vector for the elements present in the list

<source lang="cpp">

  1. include <algorithm>
  2. include <vector>
  3. include <list>
  4. include <iostream>

using namespace std; int main () {

   vector <int> v;
   for (int nNum = -9; nNum < 10; ++ nNum)
       v.push_back (nNum);
   v.push_back (9);
   v.push_back (9);
   list <int> l;
   for (int nNum = -4; nNum < 5; ++ nNum)
       l.push_back (nNum);
   vector <int>::iterator vl;
   vl = search ( v.begin ()      // Start of range
                 , v.end ()      // End of range to search in
                 , l.begin ()    // Start of range to search for
                 , l.end () );   // End of range to search for
   if (vl != v.end ()){
       cout << distance (v.begin (), vl);
       cout << endl << endl;
   }
   return 0;

}


 </source>