
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Associative arrays in C++
In c++ programming language, an associative array is a special type of array in which the index value can be of any data type i.e. it can be char, float, string, etc. These associative arrays are also known as maps or dictionaries. Also, the indexes are given a different name which is key and the data that is stored at the position of the key is value.
So, we can define the associative array as a key-value pair.
Let's define an associative array of bikes and their top speed.
Bike top speed Ninja 290 S1000rr 310 Bullet 127 Duke 135 R1 286
Example
#include <bits/stdc++.h> using namespace std; int main(){ map<string, int> speed{ { "ninja", 290 }, { "s1000rr", 310 }, { "bullet", 127 }, { "Duke", 135 }, { "R1", 286 } }; map<string, int>::iterator i; cout << "The topspeed of bikes are" << endl; for (i = speed.begin(); i != speed.end(); i++) cout<<i->first<<" "<<i->second <<endl; cout << endl; cout << "The top speed of bullet is "<< speed["bullet"] << endl; }
Output
The topspeed of bikes are Duke 135 R1 286 Bullet 127 ninja 290 s1000rr 310 The top speed of bullet is 127
- Related Articles
- Associative Arrays in PHP
- What are associative Arrays in JavaScript?
- What are multidimensional associative arrays in PHP? How to retrieve values from them?
- PHP Associative Array
- Creating an associative array in JavaScript?
- What is Associative Memory?
- What is Associative property?
- Dynamically creating keys in JavaScript associative array
- Discuss the Associative Mapping in Computer Architecture?
- How to Use Associative Array in TypeScript?
- Does division follow Associative property?
- Remove duplicated elements of associative array in PHP
- Creating an associative array in JavaScript with push()?
- Sorting an associative array in ascending order - JavaScript
- How to use associative array/hashing in JavaScript?

Advertisements