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.

Runtime & versions

Compiled native applications. Feature availability depends on the language and runtime version used by your project; consult the official reference below.

ISO C++ resources

Specifications & further reading