map - find if key exists and get value

Jan 27, 2017 at 11:03pm
I have a map and am doing a check to see if key exists and if so, get the value. Program is crashing and getting the following error from Visual Studio. "Debug Assertion Failed. map/set iterator not dereferencable".

This was the post I was using as a reference to write this code.
http://stackoverflow.com/questions/1939953/how-to-find-if-a-given-key-exists-in-a-c-stdmap

Thanks.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
  	fasta_map::iterator key; // fasta_map defined as std::map<std::string, std::string>

	std::cout << "Sample: ";
	std::cin >> sample;      //key

	while (data.find(sample) == data.end()) {
		std::cout << "Sample not found. Try again or press -1 to view list of samples: ";
		std::cin >> sample;

		if (sample == "-1") {
			get_samples();
			std::cin >> sample;
		}

	}
        //Program crashes here
	dna = data.at(sample); 
	size = dna.length();
Last edited on Jan 27, 2017 at 11:07pm
Jan 27, 2017 at 11:26pm
I don't see where you add the sample to the map.
Jan 27, 2017 at 11:30pm
The map is already defined. The variable sample, that I'm getting from the user, is the key to be used to get a value from the map. The while loop is to check if the sample(key) is in the map. get_sample() lists valid keys
Last edited on Jan 27, 2017 at 11:32pm
Jan 27, 2017 at 11:44pm
Well, nevermind. Figured it out. The error was actually somewhere else. :-/
Topic archived. No new replies allowed.