C++/Vector/vector subscript indexer

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

Create another vector that contains a subrange of vector.

   
#include <iostream>
#include <vector>
using namespace std;
void show(const char *msg, vector<int> vect);
int main() {
  vector<int> v(10);
  for(unsigned i=0; i < v.size(); ++i) v[i] = i*i;
  show("Contents of v: ", v);
  vector<int> v3;
  v3.assign(v.rbegin(), v.rend());
  show("v3 contains the reverse of v: ", v3);
  cout << endl;
  cout << "Size of v is " << v.size() << ". The capacity is "
       << v.capacity() << ".\n";
  v.resize(20);
  cout << "After calling resize(20), the size of v is "
       << v.size() << " and the capacity is "
       << v.capacity() << ".\n";
  v.reserve(50);
  cout << "After calling reserve(50), the size of v is "
       << v.size() << " and the capacity is "
       << v.capacity() << ".\n";
  return 0;
}
void show(const char *msg, vector<int> vect) {
  cout << msg << endl;
  for(unsigned i=0; i < vect.size(); ++i)
    cout << vect[i] << endl;
}


Loop thourgh all elements in a vector using

   
 
/* The following code example is taken from the book
 * "The C++ Standard Library - A Tutorial and Reference"
 * by Nicolai M. Josuttis, Addison-Wesley, 1999
 *
 * (C) Copyright Nicolai M. Josuttis 1999.
 * Permission to copy, use, modify, sell and distribute this software
 * is granted provided this copyright notice appears in all copies.
 * This software is provided "as is" without express or implied
 * warranty, and with no claim as to its suitability for any purpose.
 */
#include <vector>
#include <iostream>
using namespace std;
int main()
{
   vector<int> coll;
   // insert elements from -3 to 9
   for (int i=-3; i<=9; ++i) {
       coll.push_back (i);
   }
   /* print all elements
    * - NOTE: uses operator < instead of operator !=
    */
   vector<int>::iterator pos;
   for (pos=coll.begin(); pos<coll.end(); ++pos) {
       cout << *pos << " ";
   }
   cout << endl;
}
/* 
-3 -2 -1 0 1 2 3 4 5 6 7 8 9
 */


Loop through all elements in a vector using operator [] instead of operator *

   
 
/* The following code example is taken from the book
 * "The C++ Standard Library - A Tutorial and Reference"
 * by Nicolai M. Josuttis, Addison-Wesley, 1999
 *
 * (C) Copyright Nicolai M. Josuttis 1999.
 * Permission to copy, use, modify, sell and distribute this software
 * is granted provided this copyright notice appears in all copies.
 * This software is provided "as is" without express or implied
 * warranty, and with no claim as to its suitability for any purpose.
 */
#include <vector>
#include <iostream>
using namespace std;
int main()
{
   vector<int> coll;
   // insert elements from -3 to 9
   for (int i=-3; i<=9; ++i) {
       coll.push_back (i);
   }
   /* print all elements
    * - NOTE: uses operator [] instead of operator *
    */
   for (int i=0; i<coll.size(); ++i) {
       cout << coll.begin()[i] << " ";
   }
   cout << endl;
}
/* 
-3 -2 -1 0 1 2 3 4 5 6 7 8 9
 */


Raise all values in a vector by a given percentage

  
#include <iostream>
#include <vector>
using namespace std;
void raise_by_percent(vector<double>& v, double p)
{  
   for (int i = 0; i < v.size(); i++)
      v[i] = v[i] * (1 + p / 100);
}
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;
   raise_by_percent(salaries, 4.5);
   for (int i = 0; i < salaries.size(); i++)
      cout << salaries[i] << "\n";
   return 0;
}


Read double from keyboard, save it to a vector and find the max value

  
#include <iostream>
#include <vector>
using namespace std;
int main()
{  
   vector<double> salaries;
   bool more = true;
   while (more)
   {  
      double s;
      cout << "Please enter a salary, 0 to quit: ";
      cin >> s;
      if (s == 0)
         more = false;
      else
         salaries.push_back(s);
   }
   double highest = salaries[0];
   int i;
   for (i = 1; i < salaries.size(); i++)
      if (salaries[i] > highest)
         highest = salaries[i];
   for (i = 0; i < salaries.size(); i++)
   {  
      if (salaries[i] == highest) 
         cout << "highest value => ";
      cout << salaries[i] << "\n";
   }
   return 0;
}


Use indexer to add elements to a vector

   
 
#include <iostream>
#include <cassert>
#include <algorithm>
#include <vector>
#include <deque>
using namespace std;
int main()
{
  vector<int> vector1(20);
  for (int i = 0; i < 20; ++i)
    vector1[i] = i;
  vector<int>::iterator pos;
  for (pos=vector1.begin(); pos!=vector1.end(); ++pos) {
        cout << *pos << " ";
  }
  return 0;
}
/* 
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 
 */


Use indexer to reference elements in a vector

   
 
/* The following code example is taken from the book
 * "The C++ Standard Library - A Tutorial and Reference"
 * by Nicolai M. Josuttis, Addison-Wesley, 1999
 *
 * (C) Copyright Nicolai M. Josuttis 1999.
 * Permission to copy, use, modify, sell and distribute this software
 * is granted provided this copyright notice appears in all copies.
 * This software is provided "as is" without express or implied
 * warranty, and with no claim as to its suitability for any purpose.
 */
#include <iostream>
#include <vector>
using namespace std;
int main()
{
    vector<int> coll;    // vector container for integer elements
    // append elements with values 1 to 6
    for (int i=1; i<=6; ++i) {
        coll.push_back(i);
    }
    // print all elements followed by a space
    for (int i=0; i<coll.size(); ++i) {
        cout << coll[i] << " ";
    }
    cout << endl;
}
/* 
1 2 3 4 5 6
 */