← C++

C++ · 03 / 24

Strings, vectors and standard algorithms

Store several order IDs in a container and sort them using the standard library.

  • Small
  • Basics
  • 45 min

Prerequisites: Variables, decisions, loops and functions

In this tutorial
  1. Create a collection
  2. Use a standard sorting algorithm
  3. Print elements and size
  4. Exercise and solution
  5. Project context and sources

Download learning package

Setup and verification

GCC with C++20 and CMake 3.25 or newer. Python 3 is also required for the common verification entry point. Extract the learning package into a new folder and run python3 run_checks.py there. The script copies the included files into a temporary learning directory. It verifies the example and then the supplied solution there, preserving the original files. Required packages are downloaded on the first run.

Verified on 2026-10-21 on Linux x86_64. The console example and exercise solution are executed. Expected output is shown in the article.

The program previously used individual numbers, but a warehouse has several orders. std::vector stores a changing number of values of the same type. std::string manages text. Both belong to the C++ standard library and manage their own storage. You do not manually allocate or release memory to use them.

The expression std::vector<int> means a vector containing integers. Angle brackets identify the element type. This lesson does not introduce a database. The list exists in the running program’s memory and disappears when the program exits.

Package contents and individual files

01Create a collection

The braces contain the first three order IDs. push_back appends a fourth ID. warehouse is a string containing the warehouse name. The include directives make the standard types and algorithm available. main remains the program entry point.

main.cpp · cppOpen file
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
int main() {
  std::string warehouse = "North warehouse";
  std::vector<int> orderIds{3, 1, 2};
  orderIds.push_back(4);
  std::sort(orderIds.begin(), orderIds.end());
  std::cout << warehouse << '\n';
  for (int id : orderIds)
    std::cout << "order=" << id << '\n';
  std::cout << "orders=" << orderIds.size() << '\n';
}

Run / check

g++ -std=c++20 main.cpp -o lesson
./lesson

02Use a standard sorting algorithm

std::sort orders the IDs from smallest to largest. begin and end delimit the element range. end is the position after the final element, not a value that can be read. Passing these two boundaries is enough for the algorithm in this example.

03Print elements and size

The range-based for loop processes each element. int id receives a copy here. size reports the element count. Compile and execute the program, then compare the sorted order with the original order.

Expected result

North warehouse order=1 order=2 order=3 order=4 orders=4

Your exercise

Append the ID five and another ID two. Does the vector automatically enforce uniqueness?

Show the solution and explanation

push_back accepts both values. After sorting, the two occurrences of ID two are adjacent and size reports six. A vector allows duplicates. Unique business order IDs require separate validation, as the FactoryForge project model demonstrates. Sorting does not enforce that rule.

solution/main.cpp · cppOpen file
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
int main() {
  std::string warehouse = "North warehouse";
  std::vector<int> orderIds{3, 1, 2};
  orderIds.push_back(4);
  orderIds.push_back(5);
  orderIds.push_back(2);
  std::sort(orderIds.begin(), orderIds.end());
  std::cout << warehouse << '\n';
  for (int id : orderIds)
    std::cout << "order=" << id << '\n';
  std::cout << "orders=" << orderIds.size() << '\n';
}

Project context and sources

The first six examples are standalone teaching programs. Lessons 7 through 9 use the complete FactoryForge source snapshot. Code, exercises and verified simplifications are explained separately. The project page provides a source ZIP while the GitHub repository is not yet published.