Codectionary / Developer documentation / C++

C++ Vector & Map

std::vector is a resizable sequence container. std::map stores key-value pairs ordered by key. Both are available through standard-library headers.

Syntax

std::vector<int> values{1, 2, 3};
std::map<std::string, int> scores;

Examples

Working with standard containers

A small, runnable example of this syntax.

#include <iostream>
#include <map>
#include <vector>

int main() {
  std::vector<int> values{1, 2, 3};
  std::map<std::string, int> scores{{"Ada", 9}};
  values.push_back(4);
  std::cout << scores.at("Ada") << "\n";
}

Best practices

  • Use .at when an absent key should be treated as an error; use find when absence is an expected case.
  • Keep examples small while learning, then combine the idea with a real project.

At a glance

Purpose
Systems and general-purpose software
File extension
.cpp ยท .hpp
Runs in
Compiled native applications
Usually used with
Compiler and standard library

Specifications & further reading

Related C++ documentation