Home > Mobile >  How to count the strings in a map?
How to count the strings in a map?

Time:05-17

A method of a class returns a map of string and objects of another class. I want to count the number of strings? How can I do that?

This is the representation of the method

class b{
public:
std::map<std::string, ClassA*>& methodA();
};

I am calling this method with

auto a = pointer_of_class_B ->methodA();

This will return a map. How can I count the number of strings in this map?

CodePudding user response:

You can use the std::map::size function to find how many elements are in the map.

Cpp Reference: https://en.cppreference.com/w/cpp/container/map/size

CodePudding user response:

How can I count the number of strings in this map?

You can use std::map::size member function of std::map as shown below:

//---------------------------------------------------vvvv--->use size member function of std::map
std::cout << "number of strings inside map is " << a.size() << '\n';

This works as size member function gives the number of elements inside the map which in your case is equal to the number of strings in the map.

CodePudding user response:

You can use std:map cpp reference

  • Related