Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,939 questions

51,876 answers

573 users

How to search an element with specific value in a map using C++

3 Answers

0 votes
#include <iostream>  
#include <map>
#include <algorithm>

using std::map;
using std::cout;
using std::endl;
using std::pair;

void printMap(const map<float, float>& mp)
{
	for (auto element : mp) {
		cout << element.first << ": " << element.second << endl;
	}
	cout << endl;
}

int main()
{
	map<float, float> mp = { { 1.1, 3.14 }, { 1.7, 3.87 }, { 1.2 , 2.25 }, { 1.8, 9.0 } };

	printMap(mp);

	auto value = find_if(mp.begin(), mp.end(), [](const pair<float, float>& element) {
		return element.second == 9.0;
	});

	if (value != mp.end()) {
		cout << "value 9.0 exist {"	<< value->first << ": " << value->second << "}" << endl;
	}

	return 0;
}

/*
run:

1.1: 3.14
1.2: 2.25
1.7: 3.87
1.8: 9

value 9.0 exist {1.8: 9}

*/

 



answered Jan 12, 2018 by avibootz
0 votes
#include <iostream>  
#include <map>
#include <algorithm>
 
using std::map;
using std::cout;
using std::endl;
using std::pair;
 
void printMap(const map<float, float>& mp)
{
    for (auto element : mp) {
        cout << element.first << ": " << element.second << endl;
    }
    cout << endl;
}
 
int main()
{
    map<float, float> mp = { { 1.1, 3.14 }, { 1.7, 3.87 }, { 1.2 , 2.25 }, { 1.8, 9.99 } };
 
    printMap(mp);
 
    auto value = find_if(mp.begin(), mp.end(), [](const pair<float, float>& element) {
        return element.second == 2.25;
    });
 
    if (value != mp.end()) {
        cout << "value 2.25 exist {"  << value->first << ": " << value->second << "}" << endl;
    }
 
    return 0;
}
 
/*
run:
 
1.1: 3.14
1.2: 2.25
1.7: 3.87
1.8: 9.99
 
value 2.25 exist {1.2: 2.25}
 
*/

 



answered Jan 12, 2018 by avibootz
0 votes
#include <iostream>  
#include <map>
#include <algorithm>

using std::map;
using std::cout;
using std::endl;
using std::pair;

void printMap(const map<float, float>& mp)
{
	for (auto element : mp) {
		cout << element.first << ": " << element.second << endl;
	}
	cout << endl;
}

int main()
{
	map<float, float> mp = { { 1.1, 3.14 }, { 1.7, 3.87 }, { 1.2 , 2.25 }, { 1.8, 9.99 } };

	printMap(mp);

	auto value = find_if(mp.begin(), mp.end(), [](const pair<float, float>& element) {
		return element.second == (float)9.99;
	});

	if (value != mp.end()) {
		cout << "value 9.99 exist {"	<< value->first << ": " << value->second << "}" << endl;
	}

	return 0;
}

/*
run:

1.1: 3.14
1.2: 2.25
1.7: 3.87
1.8: 9.99

value 9.99 exist {1.8: 9.99}

*/

 



answered Jan 12, 2018 by avibootz
...