How to Use a C++ Vector to Store Data

The vector is the official array of C++. Part of the Standard Template Library, it's a template container class that stores same-typed data in an uninterrupted region of memory. As a C++ class, it offers many useful features, such as resizing, that reduce code size and save time. Its indexing operations are as efficient as those of the dumb arrays in C.

Things You'll Need

  • Advanced C++
  • C++ compiler with IDE
Show More

Instructions

    • 1

      Include the vector header file so that your program can access the C++ class and its functions:

      #include< vector >

    • 2

      Create an empty vector of type int. Then create a vector with 10 copies of 7:

      vector< int > v; // empty

      vector< int > v2(10, 7);

    • 3

      Use v2 to find the difference between how much memory has been allocated for v2 compared to its size. The memory allocated is always greater than or equal to the size:

      cout << v2.capacity() - v2.size() <

    • 4

      Add two more elements to the end of v2:

      v2.push_back(13);

      v2.push_back(23);

    • 5

      Double the size of v2 and give the additional elements a value of 64:

      v2.resize(v2.size()*2, 64);

    • 6

      Iterate across the vector using a special-purpose pointer called an iterator. Output the elements to the console as follows:

      for( vector::iterator it = v2.begin(); it != v2.end(); it++ ){

      cout << *it << " ";

      }

    • 7

      Erase elements 2 and 3 from the vector. Then erase all the elements at once:

      v2.erase(v2.begin()+1, v2.begin()+3); // erases elements 2 and 3

      v2.clear(); // erases all the elements

Related Searches:

Resources

Comments

You May Also Like

  • Vector Cross Product Rules

    The cross product of two vectors yields a third vector in three-dimensional space. The resulting vector is perpendicular to both the cross-multiplied...

  • How to Copy a File to a Vector in C++

    In the C++ programming language, a vector is a type of dynamic array -- a class that contains a series of variables,...

  • How to Put a Text File Into a Vector of Structs in C++

    The C++ Standard Template Library (STL) Vector class is a dynamic list data structure. The template design of the vector enables you...

  • How to Use the Vector STL Container Class

    A container class is a class whose instances contain other classes. The Standard Template Library (STL) for C++ is available on the...

  • How to Use the Vector in Photoshop

    Vector-based graphics types such as SVG (Scalable Vector Graphics) are the highest resolution 2D graphics available for the artist to create and...

  • How to Make Vector Eyes in Illustrator

    Whether you're a beginner or a seasoned professional in the world of art, you will benefit from learning how to illustrate using...

  • How to Save a Vector to a File in C++

    The vector class in the C++ Standard Template Library serves as an alternative to the simple array. They are almost as fast...

  • How to Create a Vector Image in Photoshop

    When people think about creating vector graphics, they tend to think of CorelDraw or Abode Illustrator. But while it is true that...

  • How to Use a Scanner for Vector Art

    Vector graphics use geometric shapes to create image files that are often very small but can be infinitely expanded without any loss...

  • How to Learn STL Containers

    The container classes of STL make programming productive, safe and robust. They're smart arrays that perform memory allocation automatically, can resize, let...

  • How to Do a Vector Block

    Vector blocks are made by creating 3D cubes. You can use a 3D software like Maya or 3DMax to draw blocks, but...

  • How to Use C Arrays in C++

    A C array is a data structure that stores many elements of the same data type. C arrays, when you use them...

  • How to Create Vector Art

    Vector art is created by manipulating a photo or image with a computer program. It makes the image appear in an animated...

  • How to Create an Array in C

    Arrays offer the most efficient method for storing lists of data in C. They're very easy for the programmer to create and...

  • How to Create 3D Text in Inkscape

    Inkscape provides many tools to create interesting 3D effects in two-dimensional drawings. In addition to its primary features, Inkscape includes many contributed...

  • Vector Data File Types

    Vector Data File Types. Vector data files store the mathematical formulas needed to draw a particular image. Unlike raster data files (such...

  • How to Use the Set STL Container Class

    Container classes are a tool used in C and C++ programming to use as a repository of any kind of data. The...

  • How to Create a Vector File

    Vector files are a type of computer graphics that can be created from scratch by an artist. You use drawing tools, lettering...

  • How to Write a Queue Class in C++

    A queue is a first-in, first-out set of the elements added and removed from both ends sequentially. Queues receive and process information...

Related Ads

Featured