Trouble with populating a map

I'm trying to read in a text file and then populate a map using the data. My code seems to be reading in the data correctly, but it always gets stuck on the line that inserts a new pair into the map. No errors messages, it just never passes the line of code that creates a new pair in the map.

The map is defined in the header file:
std::map<std::string, CELL > cell_map;

Where CELL is a struct of type:
1
2
3
4
5
6
7
8
9
10
 struct CELL {
	int numrings;
	double mesh_x_minus;
	double mesh_x_plus;
	double mesh_x;
	double mesh_y_minus;
	double mesh_y_plus;
	double mesh_y;
	std::vector< double > rings;
};


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
cell_type = inpL[1]; //inpL is a vector of strings containing the current line of the text  file
				
// Populate cell
cell.numrings = converter->StringToInt(inpL[5]);
inpL = ProcessLine(input, inpL); //Reads in the next input line of the text file
cell.mesh_x_minus = converter->StringToDouble(inpL[1]);
cell.mesh_x_plus = converter->StringToDouble(inpL[2]);
cell.mesh_x = cell.mesh_x_plus-cell.mesh_x_minus;

cell.mesh_y_minus = converter->StringToDouble(inpL[4]);
cell.mesh_y_plus = converter->StringToDouble(inpL[5]);
cell.mesh_y = cell.mesh_y_plus-cell.mesh_y_minus;
				
inpL.clear();
inpL = ProcessLine(input, inpL);
cell.rings.clear();
							
std::cout << " I got here" << std::endl;
cell_map[cell_type] = cell; // Where the code gets stuck
std::cout << " I got here too" << std::endl; 


This code prints "I got here" but not "I got here too". I've tried hard coding in all the variables as a test, instead of reading them in from the text file, but it still gets stuck at the same point. Any suggestions for where the error might be?

Last edited on
> I've tried hard coding in all the variables as a test, instead of reading them in from the text file,
> but it still gets stuck at the same point. Any suggestions for where the error might be?

Doesn't have anything to do with inserting into the map; when I try it it works flawlessly. Just guessing - a memory corruption somewhere earlier, perhaps?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>
#include <map>
#include <string>
#include <vector>
#include <cassert>

struct CELL
{
	int numrings;
	double mesh_x_minus;
	double mesh_x_plus;
	double mesh_x;
	double mesh_y_minus;
	double mesh_y_plus;
	double mesh_y;
	std::vector< double > rings;
};

int main()
{
    std::map< std::string, CELL > cell_map ;
    CELL a{ 10, 20, 30, 40, 50, 60, 70, { 80, 90, 100, 110, 120 } } ;
    CELL b{ 11, 21, 31, 41, 51, 61, 71, { 81, 91, 101, 111, 12 } } ;

    cell_map["aaa"] = a ;
    assert( !cell_map.empty() ) ;
    auto result = cell_map.insert( std::make_pair( "bbb", b ) ) ;
    assert( result.second && ( cell_map.size() == 2U ) ) ;

    for( const auto& p : cell_map )
        std::cout << '{' << p.first << ',' << p.second.numrings << "}\n" ;
}

output:
{aaa,10}
{bbb,11}
Last edited on
It would be nice if C++11 specific code were marked as C++11 specific.
Thanks for your help. Could you please explain to me what this is doing?:
1
2
3
assert( !cell_map.empty() ) ;
    auto result = cell_map.insert( std::make_pair( "bbb", b ) ) ;
    assert( result.second && ( cell_map.size() == 2U ) ) ;


I'm pretty new to C++, so I'm still trying to get the hang of it.

Also, I tried declaring the map right before I populate it and everything works fine. Unfortunately, I need to be able to access it in another Class, so I believe I need to define it in the header file. Right now it's a private variable, which didn't think mattered, but I really don't know. Again, I'm pretty new so I'm still learning how everything works together. Any suggestions?
Last edited on
The code there is C++11 code, which you probably haven't learned at all yet (C++11 is the new version of C++ announced in 2011)
Line one is an assertion to make sure the map is not empty.
Line two makes a variable called result, and its type is automatically determined.
Line three is an assertion to make sure that the second part of the pair that result is is OK and that the cell map's size is equal to 2U.
> I tried declaring the map right before I populate it and everything works fine. ...
> ... Any suggestions?

Difficult to give any suggestions without knowing a bit more about the problem.

Let us start with this: Are you trying to populate the map before the first statement in main() is executed?


Last edited on
In short, no.

I'm trying to read a text file, store certain parts of it, and then output to another text file. My code is set up in a hierarchical manner. The main program calls my datahandler, which then calls different functions based on keywords it reads in from the text file. The map is being populated in one of those functions. I then need to access that map in another function in my datahandler class that outputs to a text file.

When I declare the map as a static variable in the .cc file of the function, everything works when populating the map but I can't access it once my code returns to the datahandler. If I declare the it in the header file, the code gets stuck when I try to insert pairs into the map.



> If I declare the it in the header file, the code gets stuck when I try to insert pairs into the map.

Is this the case?

The header file has the declaration: extern std::map< std::string, CELL > cell_map ;

And the implementation file has its definition: std::map< std::string, CELL > cell_map ;
No, its only declared once.

I've never used the extern keyword. I've looked it up, but I'm still unsure how to use it. Right now, cell_map is a private variable.
There is no such thing as a 'private variable' unless it is in the private datazone of a class.
it's declared in the private datazone of the class, in the header file.
Last edited on
i just ran my code using the debugger gdb. it's says the problem is in std::string::compare

the keys are strings, so shouldn't the comparison operator already be defined?
The problem is not that std::string::compare() is not defined - you would have got a linker error if that was the case. It seems to be either

a. The object which has std::map<std::string, CELL > cell_map; has not been constructed; so the map has not been initialized. Are you accessing this object through a pointer? Is the pointer being initialized at the point of definition?
myclass* pointer = new myclass( /*...*/ ) ;
or
1
2
3
myclass* pointer = nullptr ;// initialize to null
// ...
pointer = new myclass( /*...*/ ) ; // assign non-null value before using it  


b. The std::string specified as the key is invalid. This is unlikely as the code works as expected when the map is not the member of an object. In any case, just to be defensive, before
cell_type = inpL[1]; //inpL is a vector of strings containing the current line of the text file add a check: assert( inpL.size() > 1U ) ;
that was it. uninitialized pointer. thanks for all the help!
Topic archived. No new replies allowed.