C++ Tutorial/vector/vector

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

Add class to a vector and then delete them one by one

<source lang="cpp">#include <iostream>

  1. include <vector>

using namespace std; class MyClass { }; int main( ) {

  vector<MyClass*> vec;
  MyClass* p = NULL;
  for (int i = 0; i < 10; i++) {
     p = new MyClass( );
     vec.push_back(p);
  }
  for (vector<MyClass*>::iterator pObj = vec.begin( );
       pObj != vec.end( ); ++pObj) {
     delete *pObj;
  }
  vec.clear( );

}</source>

Assign elements in int array to vector

<source lang="cpp">#include <iostream>

  1. include <vector>
  2. include <cassert>
  3. include <numeric> // for accumulate

using namespace std; int main() {

 int x[5] = {2, 3, 5, 7, 11};
 vector<int> vector1(&x[0], &x[5]);
 int sum = accumulate(vector1.begin(), vector1.end(), 0);
 cout << sum << endl;
 return 0;

}</source>

28

Assign value to the last element

<source lang="cpp">/* 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.
*/
  1. include <iostream>
  2. include <vector>
  3. include <string>
  4. include <algorithm>
  5. include <iterator>

using namespace std; int main() {

   // create empty vector for strings
   vector<string> sentence;
   // reserve memory for five elements to avoid reallocation
   sentence.reserve(5);
   // append some elements
   sentence.push_back("Hello,");
   sentence.push_back("how");
   sentence.push_back("are");
   sentence.push_back("you");
   sentence.push_back("?");
   // assign "!" to the last element
   sentence.back() = "!";
   // print elements separated with spaces
   copy (sentence.begin(), sentence.end(),
         ostream_iterator<string>(cout," "));
   cout << endl;

}</source>

Hello, how are you !

Computing an inner product of tuples represented as vectors

<source lang="cpp">#include <vector>

  1. include <iostream>

using namespace std; int main() {

 const long N = 600000; // Length of tuples x and y
 const long S = 10;     // Sparseness factor
 vector<double> x(N), y(N);
 for (long k = 0; 3 * k * S < N; ++k)
   x[3 * k * S] = 1.0;
 for (long k = 0; 5 * k * S < N; ++k)
   y[5 * k * S] = 1.0;
 double sum = 0.0;
 for (long k = 0; k < N; ++k)
   sum += x[k] * y[k];
 cout << sum << endl;
 return 0;

}</source>

4000

creates a vector of 10 ints with value 100

<source lang="cpp">#include <vector> using namespace std; int main(int argc, char** argv) {

 vector<int> intVector(10, 100); // creates a vector of 10 ints with value 100
 return (0);

}</source>

creates a vector of ints with zero elements

<source lang="cpp">#include <vector> using namespace std; int main(int argc, char** argv) {

 vector<int> intVector; // creates a vector of ints with zero elements
 return (0);

}</source>

Demonstrating STL vector constructors with a user-defined type

<source lang="cpp">#include <iostream>

  1. include <cassert>
  2. include <vector>

using namespace std; class MyClass { public:

 unsigned long id;
 MyClass() : id(0) { }
 MyClass(unsigned long x) : id(x) { }

}; bool operator==(const MyClass& x, const MyClass& y) {

 return x.id == y.id;

} bool operator!=(const MyClass& x, const MyClass& y) {

 return x.id != y.id;

}

int main() {

 vector<MyClass> vector1, vector2(3);
 assert (vector1.size() == 0);
 assert (vector2.size() == 3);
 assert (vector2[0] == MyClass() && vector2[1] == MyClass() &&vector2[2] == MyClass());
 assert (vector2 == vector<MyClass>(3, MyClass()));
 return 0;

}</source>

Demonstrating STL vector constructors with a user-defined type and showing copying explicitly

<source lang="cpp">#include <iostream>

  1. include <cassert>
  2. include <vector>

using namespace std; class MyClass { public:

 unsigned long id;
 unsigned long generation;
 static unsigned long total_copies;
 MyClass() : id(0), generation(0) { }
 MyClass(unsigned long n) : id(n), generation(0) { }
 MyClass(const MyClass& z) : id(z.id), generation(z.generation + 1) {
   ++total_copies;
 }

}; bool operator==(const MyClass& x, const MyClass& y) {

 return x.id == y.id;

} bool operator!=(const MyClass& x, const MyClass& y) {

 return x.id != y.id;

} unsigned long MyClass::total_copies = 0; int main() {

 vector<MyClass> vector1, vector2(3);
 assert (vector1.size() == 0);
 assert (vector2.size() == 3);
 assert (vector2[0] == MyClass() && vector2[1] == MyClass() &&
         vector2[2] == MyClass());
 for (int i = 0; i != 3; ++i)
   cout << "vector2[" << i << "].generation: " << vector2[i].generation << endl;
 cout << "Total copies: " << MyClass::total_copies << endl;
 return 0;

}</source>

vector2[0].generation: 1
vector2[1].generation: 1
vector2[2].generation: 1
Total copies: 3

Demonstrating STL vector copying constructors

<source lang="cpp">#include <iostream>

  1. include <cassert>
  2. include <vector>

using namespace std; int main() {

 char name[] = "abcedfghijklmn";
 vector<char> George(name, name + 6);
 vector<char> son1(George); // Uses copy constructor
 assert (son1 == George);
 return 0;

}</source>

Demonstrating the simplest STL vector constructors: duplicate chars

<source lang="cpp">#include <iostream>

  1. include <cassert>
  2. include <vector>

using namespace std; int main() {

 vector<char> vector2(3, "x");
 assert (vector2.size() == 3);
 assert (vector2[0] == "x" && vector2[1] == "x" && vector2[2] == "x");
 assert (vector2 == vector<char>(3, "x"));
 return 0;

}</source>

Demonstrating the simplest STL vector constructors: empty vector

<source lang="cpp">#include <iostream>

  1. include <cassert>
  2. include <vector>

using namespace std; int main() {

 vector<char> vector1;
 cout << vector1.size();
 return 0;

}</source>

0

get an iterator that points to the maximum with max_element

<source lang="cpp">#include <algorithm>

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

using namespace std; int main( ) {

  vector<int> v( 10, 66 );
  v[1] = 100;
  // get an iterator that points to the maximum
  vector<int>::iterator before_itr = max_element( v.begin(), v.end() );
  cout << "The maximum before resizing is " << *before_itr;

}</source>

Instantiate one vector and initialize it to the contents of another

<source lang="cpp">#include <vector> int main () {

  std::vector <int> v;
  // Instantiate a vector with 10 elements (it can grow larger)
  std::vector <int> v1 (10);
  // Instantiate a vector with 10 elements, each initialized to 90
  std::vector <int> v2 (10, 90);
  // Instantiate one vector and initialize it to the contents of another
  std::vector <int> v3 (v2);
  return 0;

}</source>

Pass vector to a function

<source lang="cpp">#include <iostream> using std::cout; using std::endl;

  1. include <vector>

using std::vector; template < typename T > void printVector( const vector< T > &integers2 ); int main() {

  int array[ 6 ] = { 1, 2, 3, 4, 5, 6 }; // initialize array
  vector< int > integers; // create vector of ints
  integers.push_back( 2 );
  integers.push_back( 3 );
  integers.push_back( 4 );
  printVector( integers );
  cout << endl;
  return 0;

} template < typename T > void printVector( const vector< T > &integers2 ){

  typename vector< T >::const_iterator constIterator;
  for ( constIterator = integers2.begin();
     constIterator != integers2.end(); ++constIterator )
     cout << *constIterator << " ";

}</source>

2 3 4

Read keyboard input to a vector

<source lang="cpp">/* 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.
*/
  1. include <iostream>
  2. include <string>
  3. include <algorithm>
  4. include <iterator>
  5. include <vector>

using namespace std; int main() {

   /* create a string vector
    * - initialized by all words from standard input
    */
   vector<string> coll((istream_iterator<string>(cin)),
                       istream_iterator<string>());
   // sort elements
   sort (coll.begin(), coll.end());
   // print all elements ignoring subsequent duplicates
   unique_copy (coll.begin(), coll.end(),
                ostream_iterator<string>(cout, "\n"));

}</source>

Terminate batch job (Y/N)? n

The Instantiation of vector<bool>

<source lang="cpp">#include <vector> int main () {

   using namespace std;
   // Instantiate an object using the default constructor
   vector <bool> vecBool1;
   // A vector of 10 elements with value true (default: false)
   vector <bool> vecBool2 (10, true);
   // Instantiate one object as a copy of another
   vector <bool> vecBool2Copy (vecBool2);
   return 0;

}</source>

Use generic vector to create vector of chars

<source lang="cpp">#include <iostream>

  1. include <cassert>
  2. include <vector>
  3. 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;

}</source>

b

Use generic vector to create vector of integers

<source lang="cpp">#include <iostream>

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

using namespace std;

int main() {

 int x[5] = {2, 4, 6, 8, 10};
 vector<int> vector1(&x[0], &x[5]);
 // Search for the first occurrence
 vector<int>::iterator where = find(vector1.begin(), vector1.end(), 4);
 cout << *where  << endl;
 return 0;

}</source>

4

Use generic vector to create vector of strings

<source lang="cpp">// Demonstrating the generic find algorithm with a vector

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

using namespace std;

int main() {

 string x[5] = {"1234", "2345", "3456", "4567", "0987"};
 vector<string> vector1(&x[0], &x[5]);
 // Search for the first occurrence
 vector<string>::iterator where = find(vector1.begin(), vector1.end(), "1234");
 cout << *where  << endl;
 return 0;

}</source>

1234

Using the vector<bool>

<source lang="cpp">#include <vector>

  1. include <iostream>

int main () {

   using namespace std;
   // Instantiate a vector<bool> to hold 3 elements
   vector <bool> vecBool (3);
   // Assign 3 elements using the array operator []
   vecBool [0] = true;
   vecBool [1] = true;
   vecBool [2] = false;
   // Insert a 4th element using push_back:
   // this will cause the vector to resize the buffer
   vecBool.push_back (true);
   cout << "The contents of the vector are: " << endl << "{";
   for (size_t nIndex = 0; nIndex < vecBool.size (); ++ nIndex)
       cout << vecBool [nIndex] << " ";
   cout << "}" << endl << endl;
   vecBool.flip ();
   cout << "The flipped contents of the vector are: " << endl << "{";
   for (size_t nIndex = 0; nIndex < vecBool.size (); ++ nIndex)
       cout << vecBool [nIndex] << " ";
   cout << "}";
   return 0;

}</source>

Vector Init Array

<source lang="cpp">#include <iostream>

  1. include <vector>

using namespace std; void print( vector<int> ); int main() {

   int data[] = {11, 12, 23, 34};
   int size = sizeof( data ) / sizeof( data[0] );
   vector<int> vec( data, &data[ size ] );
   print( vec );           // 11 12 23 34
   return 0;

} void print( vector<int> vec ) {

   vector<int>::iterator p = vec.begin();
   while ( p < vec.end() )
     cout << *p++ << " ";
   cout << endl;

}</source>