Loading...

C++ vector

The nullptr page left one line on the screen in box.cpp: pine, after p = &word; and printing *p. This page stays in the Memory and Collections module on purpose. The workshop file on disk stays box.cpp so you keep one familiar build path. The new work stores strings in a growing list called a vector, then prints the first item at index zero. The screen still shows one line: pine after that print.
A vector holds a sequence of values of one type that can grow as you add items. On this page you write std::vector<std::string> words; and then words.push_back("pine");. The type inside the angle brackets is the item type for every slot in that list. Then std::cout << words[0] << std::endl; prints the first string at index zero. Reading push_back is reading an add at the end of the list. Reading words[0] is reading the item that now lives at index zero after that add.
Programs use vector when you need a list that can grow instead of a fixed array size chosen too early. You do not keep the pointer p as the lesson path on this page, even though both tools live in the same module. Extra methods like size and clear wait for the next page on purpose. Here the point is only creating the vector, adding one string with push_back, and printing index zero. The build command stays familiar: one source file box.cpp, one output name box, one run that prints pine. Keep #include <iostream>, #include <string>, and #include <vector> together at the top. You compile only box.cpp again for this workshop file on disk.

Inside main, write std::vector<std::string> words;, then words.push_back("pine");, then std::cout << words[0] << std::endl;, then return 0;. Keep the three includes at the top of the file. Do not print through a pointer on this page. Do not leave the vector empty and still read words[0] as if an item were already there.
Walk the run carefully from the first line of main. The program starts at main. words starts empty with no items yet. push_back("pine") adds one string at the end, so index zero holds pine after the call. Printing words[0] shows pine on the screen. Then main returns and the program ends. There is one line on the screen. It is pine, not an empty line. The empty start of the vector matters on purpose: you add before you read index zero. The checked file always calls push_back before words[0] is printed in this workshop.

If you read words[0] before any push_back, the program is unsafe. That failed experiment stays in the prose. The checked file always adds pine first.
If you forget #include <vector>, the build fails about std::vector. The checked file keeps that include next to iostream and string.
A common mistake is to write words.push_back without parentheses and the string. Another common mistake is to use the wrong index and read an empty slot. A third mistake is to keep the old nullptr pointer body and never add the vector. A fourth mistake is to print a literal "pine" and never use words[0]. A fifth mistake is to drop #include <string> while using std::string inside the vector type.
This page stops before the next tool, where more vector methods do work on the list. The next page names that tool once at the end. The vector you have just written only shows one add and one print at index zero in the same function. Keep the add before the read on purpose so the order stays clear for a first careful reading of the file.

Open box.cpp. Remove the pointer lines from the nullptr demo. Add #include <vector> beside the other includes. In main, create words, call push_back("pine"), and print words[0] as the only screen line. The file name on disk stays box.cpp. The checked file is below, and the new lines are the vector type, the push call, and the index print.

box.cpp stores pine in a vector and prints words[0]

cpp
#include <iostream>
#include <string>
#include <vector>

int main()
{
    std::vector<std::string> words;
    words.push_back("pine");
    std::cout << words[0] << std::endl;
    return 0;
}

Rebuild with g++ -std=c++17 box.cpp -o box, then run ./box. If g++ is missing and clang++ is the compiler you installed, swap only the tool name: clang++ -std=c++17 box.cpp -o box. The screen shows one line: pine.
Check that line first on the screen. The vector held pine at index zero after push_back finished. The table gives each row its piece, so compare your run with it line by line before you move on to experiments. If the screen is blank, the print may be missing from main. If the build fails about vector, add #include <vector> next to the other includes. If you crash on words[0], put push_back back before the read so index zero exists.

📊 vector and what the screen shows

Piece What the screen shows
First pine, because push_back stores pine at index zero
Last pine, because the program prints one line and stops
Rule a vector holds a growing list; push_back adds; [0] reads the first item
Screen pine

Try one change at a time. Change push_back("pine") to push_back("oak"), leave the print as words[0], and rebuild. The screen shows oak instead of pine, because only the stored string at index zero changed. Put "pine" back into the push. Next, after the first push, add words.push_back("cone"); and temporarily print words[1] instead of words[0], then rebuild to see cone on the screen. Restore the single-item checked shape before you leave so the file matches the lesson again. Each experiment changes one thing on purpose, so the screen shows exactly which index held which string value. Undo each change before the next one starts, so the file always returns to the checked workshop version with one push_back and words[0] once again after every single careful try back on disk again.
This page ends the first vector tool and stops before the next one. The next page is vector-methods, which names more work you can do on the list. The vector you have just written only shows adding pine and printing index zero in box.cpp. You leave with one line on the screen: pine. Count the lines before you leave. There is exactly one printed line on that final screen, and that single printed line on the screen is still just pine alone right there now after the final successful build run on disk today now after the add.

🧠 Test Your Knowledge

Ready to Start

Test Your Knowledge

Challenge yourself with this interactive quiz and see how well you understand the topic

❓
4
Questions
🎯
70%
To Pass
♾️
∞
Time
🔄
∞
Attempts

📝 Instructions

  • Read each question carefully
  • Select the best answer for each question
  • You can retake the quiz as many times as you want
  • Your progress will be shown at the top