Loading...

C++ vector Methods

The vector page left one line on the screen in box.cpp: pine, after words.push_back("pine"); and printing words[0]. 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 asks the vector how many items it holds after the push. The screen shows one line: 1.
A vector method is a call on the vector object that does work for that list. On this page you still write words.push_back("pine");, then you print words.size() instead of words[0]. The value pine still lives in the list at index zero. Reading size() is reading the count of items now stored. Reading that count as 1 proves the push added exactly one string.
Programs use methods like size so you can check the list before you read an index. You do not drop push_back on this page. Extra containers like map wait for the next page on purpose. Here the point is only adding pine and printing the size count. The build command stays familiar: one source file box.cpp, one output name box, one run that prints 1. Keep #include <iostream>, #include <string>, and #include <vector> together at the top. You compile only box.cpp again for this workshop file on disk.
Think of size() as a question you ask the vector after you change it. After one careful push_back("pine"), that question answers with 1. The answer is a number, not the string pine. The previous page already proved pine sits at index zero. This page proves the count rose by one. Both facts stay true in the same list at the same time. You choose which fact to print. On this page you choose the count so the screen line is the digit 1.

Inside main, write std::vector<std::string> words;, then words.push_back("pine");, then std::cout << words.size() << std::endl;, then return 0;. Keep the three includes at the top of the file. Do not print words[0] as the lesson path on this page. Do not call size before any push_back if you want the checked screen of 1.
Walk the run carefully from the first line of main. The program starts at main. words starts empty with size zero. push_back("pine") adds one string, so size() becomes 1. Printing words.size() shows 1 on the screen. Then main returns and the program ends. There is one line on the screen. It is 1, not pine. The string pine is still inside the vector; this page chooses to show the count. The checked file always pushes before it prints size().
Say the steps out loud once while you read the file. Empty vector. Push pine. Ask size. Print that answer. Return. That short story is the whole workshop path. Nothing else belongs in the checked body. No second push. No index print. No extra include beyond the three you already keep. When the screen shows 1, the story matched the source.

If you print size() before any push_back, the screen shows 0. That experiment can stay in the prose. The checked file always pushes pine first so the screen is 1.
If you forget the parentheses and write words.size without calling it, the build fails. The checked file always writes words.size().
A common mistake is to keep printing words[0] and never call size. Another common mistake is to push two strings and still expect the screen to show 1. A third mistake is to leave the old pointer body and never use the vector methods. A fourth mistake is to print a literal 1 and never call size. A fifth mistake is to drop #include <vector> while still using std::vector.
This page stops before the next tool, where keys map to values. The next page names that tool once at the end. The size call you have just written only shows counting items after push_back. Keep the push before the size print so the order stays clear for a first careful reading of the file.
Remember that size() does not remove pine and does not rename the vector. It only reports how many items are present right now. After one push the report is 1. After you undo an oak experiment the report returns to 1. After you restore the checked print line the screen returns to that same single digit. Stability of the checked file matters as much as learning the call itself.

Open box.cpp. Keep the vector includes and the words variable. Keep push_back("pine"). Change the print from words[0] to words.size(). The file name on disk stays box.cpp. The checked file is below, and the new line is the size() print after the push.

box.cpp prints words.size after push_back pine

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

int main()
{
    std::vector<std::string> words;
    words.push_back("pine");
    std::cout << words.size() << 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: 1.
Check that line first on the screen. The vector held one item after push_back, so size() printed 1. 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 shows 0, the push may be missing. If the screen shows pine, you still print words[0] instead of size(). If the build fails about size, check the parentheses on the call.

📊 size and what the screen shows

Piece What the screen shows
First 1, because push_back added one item before size was printed
Last 1, because the program prints one line and stops
Rule size() returns how many items the vector holds after push_back
Screen 1

Try one change at a time. Add a second words.push_back("oak"); after the pine push, leave the print as words.size(), and rebuild. The screen shows 2 instead of 1, because the count grew. Remove the oak push so the file returns to one item. Next, temporarily print words[0] again to see pine still stored, then restore words.size() so the checked screen is 1 again. Each experiment changes one thing on purpose, so the screen shows either the count or the stored string. Undo each change before the next one starts, so the file always returns to the checked workshop version with one push_back("pine") and words.size() once again after every single careful try back on disk again.
This page ends the vector-methods tool and stops before the next one. The next page is map, which stores values under keys. The size call you have just written only shows counting after adding pine in box.cpp. You leave with one line on the screen: 1. 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 1 alone right there now after the final successful build run on disk today now after the size call.

🧠 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