Insert map into vector fails second time - c++

I have an error I couldn't figure where it occues on the following:
I'm acutally using a map with vectors in it:
map<vector<string> , vector<string> > parameterMap;
because I need a few of them (how many is decided on runtime) I put them into a list (vector):
vector declaration on head of method:
vector<map<vector<string> , vector<string> > > listedParameterMap;
insertion of a map into the vector:
listedParameterMap.insert(listedParameterMap.end(), 1, parameterMap);
This procedure works fine on the first time. The second time (map is filled correctly) it down't work.
I noticed a thing: I give out the size of the map:
cout << "listedParameterMap " << listedParameterMap.size();
it shown size is 2 after the second time, the watch says it still 1.
It also shows me wired content:
Screenshot:
Last should contain something looking like First
The second map which is inserted is defently filled correctly.
Same for the vectors: part1_input and part2_output
Code:
for (unsigned int index = 0; index < part1_input.size(); index++) {
map<vector<string> , vector<string> > parameterMap;
parameterMap.insert
(pair<vector<string> , vector<string> > (part1_input[index], part2_output[index]));
listedParameterMap.insert(listedParameterMap.end(), 1, parameterMap);
cout << "listedParameterMap " << listedParameterMap.size();
}
I really would appreciate any ideas why this happens...
EDIT:
"Solution" was printing the stuff out. The watch-window isn't displaying the correct values. That means my Problem is caused somewhere else. But this here is anwsered. Thanks to anyone how tried to help me!

I would like to see a test where you output something from your collections to see if you are seeing what you think you should see. Actually why not write a proper unit test?
You are passing a lot of collections around by value. This can be quite expensive, but in addition, you may be updating something that is a copy of what you think you are actually updating, and then not seeing the results in the original.
I would not pay too much attention to values in Visual Studio's "watch" window, particularly if you are running an optimised build.

Related

Can anyone help me make this function more efficient

So I am trying to sort through an unordered_map container. The container reads input from a file which is a list of people. Each line in the file will be like rCB, bIA, and this will be stored as an element in the map. The second string in each element acts as a pointer to the next person in the list, so later on it will appear again in a new line, in this case:bIA,TDV.
So far I can sort through in order by creating an unordered_map iterator and using the second string in the find function for the iterator to go to the next element. My problem is going the other way. I am able to sort through the opposite way but the way i have implemented my solution means that it takes a very long time to eventually sort through, as we have input files of 3 million people.
list<string> SortEast(unordered_map<string, string> &TempUMap, unordered_map<string, string>::iterator IT, list<string> &TempList)
{
IT = TempUMap.begin();
while (TempList.size() != (TempUMap.size() + 1))
{
if (IT->second == TempList.front())
{
TempList.emplace_front(IT->first);
IT = TempUMap.begin();
}
IT++;
}
return TempList;
}
I've tried to make this more efficient but I cannot think of how. If i could find the value that would go at the start of the list I could sort in order starting with that value, but again I dont know how I would find this value easily.
Any help would be appreciated.
EDIT:
A sample of one of our input is:
rBC,biA
vnN,CmR
CmR,gnz
Dgu,OWn
lnh,Dgu
OWn,YMO
YMO,SIZ
XbL,Cjj
TDV,jew
iVk,vnN
wTb,rBC
jew,sbE
sbE,iVk
Cjj,wTb
AGn,XbL
gnz,SMz
biA,TDV
SIZ,uvD
SMz,lnh
This is only 20 people. In this case AGn is the first value and uvD is the last. The output I end up with is:
AGn
XbL
Cjj
wTb
rBC
biA
TDV
jew
sbE
iVk
vnN
CmR
gnz
SMz
lnh
Dgu
OWn
YMO
SIZ
uvD
As this file starts with rBC, that is the point at which i need to sort backwards
Can you not simply do something like this:
vector<string> orderAllTheNames(const unordered_map<string, string>& input, const string& begin)
{
vector<string> result;
result.reserve(input.size());
string current = begin;
result.push_back(current);
while(result.size() < input.size())
{
current = input[current];
result.push_back(std::move(current));
}
return result;
}
I may have missed some details as I typed this on my phone. You can add some pointers and/or std::moves if you're worried about too many copies flying around.
I guess it's the same as your solution, but without the awkward list and emplace_front.

C++: Iterate using enum type

I have these codes:
for (i = 0; i <= WND_WRL; i++) {
syslog(LOG_ERR, "TESTE i=%d WND_WRL=%d", i,WND_WRL);
}
for (i = 0; i <= WND_WRL; i++) {
syslog(LOG_ERR, "OnScrDsp for i=%d WND_WRL=%d", i,WND_WRL);
m_pWnd[i] = gtk_window_new(GTK_WINDOW_POPUP);
assert(m_pWnd[i]);
}
The first for is only to explain my problem. The second is really my problem.
The source of second code can be found here:
https://github.com/HuayraLinux/intel-classmate-function-keys/blob/master/OnScrDsp.cpp
The problem:
WND_WRL variable came from
typedef enum {
WND_BRG,
WND_DSP,
WND_WRL,
} WND_ID;
struct.
In first code I can see i iterate until 2 (0,1,2) and WND_WRL will be always 2. The problem is in second code: even WND_WRL ever print 2 value, that for will iterate i until receive SIGV signal (11) and break my application (here it stop with i=384). I can understand why 384, I am not concerned about that.
What I do not understand is why the same condition provide different ways. If I change WND_WRL to number 2, I get correct code and correct app execution.
My first idea is the block of the second for maybe change WND_WRL value, but isn't happened.
I can understand if may be this code is writing in wrong memory position, but I always see WND_WRL with 2 value.
SOLUTION :
Change expression "i <=WND_WRL" to "i < WND_WRL" because m_pWnd size. It explain SIGV, but not explain why for continue until receive SIGV even if 2<=2 condition matches. Overriding memory we know can destroy a lot of things, but constants and code are read-only stack memory region, so access m_pWnd[3] and others i++ not explain why for does not stop.
Variable m_pWnd is defined in your source code as an array of pointers, with a size of 2, so valid index is 0 or 1.
GtkWidget *m_pWnd[WND_WRL];
But your loop goes i <= WND_WRL, so i=2 case will crash
m_pWnd[i] = gtk_window_new(GTK_WINDOW_POPUP);

C++ Vector push / pop

I've been looking all over for a solution to this. Not using c++11.
for(int a = 1; a < team1.chan; a++)
{
team1.nums.push_back(ppb.back());
ppb.pop_back();
cout << team1.nums[a] << " " << endl;
}
ppb is an uns int vector with 1-1000 that have been shuffled.
team1 is a struct with nums as an uns int vector.
I'm trying to take the last number in ppb and assign it to the first number in team1.nums.
Then I need to delete that value in ppb so I have no duplicates.
I printed the actual numbers in ppb and they are fine. When I compile I get about 40 numbers like 2397295 then about 80 zeroes.
I am slowly getting C++, but vectors are killing me. Thank you.
Vectors are zero indexed but your 'a' starts at 1.
So the first value from ppb.back() is stored at team1.nums[0] but you print team1.nums[1].
The next value from ppb.back() is stored at team1.nums[1] but now you print team1.nums[2].
Try to loop using
while(ppb.empty() == false)
{
...// Your code here
}
I think that all you need is
team1.nums.assign( std::make_move_iterator( team1.rbegin() ), std::make_move_iterator( team1.rend() ) );
team1.clear();
Or if the move constructor is not supported then
team1.nums.assign( team1.rbegin(), team1.rend() );
team1.clear();
It is better to use iterators when using containers. It will avoid such indexing errors. Also you have access to some neat functions like std::copy which puts all the copy code you wrote in just one line.
std::copy(ppb.rbegin(),ppb.rend(),back_inserter(team1.nums));
back_inserter uses the vector::push_back so you dont have to reserve the space

c++ stack maze solving program

I wrote a program to generate a maze and to solve it, however it has some bug in the solving part.
It forms a square maze like 5*5 or 16*16.
The maze starts at (0,0 in a 2D array, and ends at (size()-1, size()-1).
I use '1' to indicate the path to end.
You can see from the picture below there are some unwanted '1' although the program can find the exit.
Excuse me everyone, I really can't debug this. Can anyone help me or guide me?
Much appreciated!
The screencapure is here. Im not allowed to post image directly
https://photos-1.dropbox.com/t/0/AADjdwSgmLdVKCZrI1C-gDvwZ9ORj0rGbv3UJ7AYqXWeuA/10/7014161/png/2048x1536/2/1355295600/0/2/bug.png/5sQR3E_jcow4lWIy9cFf2FYbmwl0C_sd2cfCyMPe0MU
My code is in here
https://www.dropbox.com/s/vldkcv4fy6bp1ff/Source.cpp
PROBLEM SOLOVED
Thanks everyone
my original code for solving the maze was
`else if (randomNum==1) {
if (y+1<myMaze.size() && !myMaze[x][y+1].left && !myMaze[x][y+1].visited)
{
y++;
myMaze[x][y].truePath=true;
myMaze[x][y].visited=true;
s1.push(myMaze[x][y]);
randomNum=rand()%4;
}
else
{
rightBusted=true;
randomNum=rand()%4;
}`
Then I just add these codes inside the if-statement to reset the bool variables to false, then the problem solved
downBusted=false;
rightBusted=false;
topBusted=false;
leftBusted=false;
You can debug this, man. Here's how. You already have the tools you need to do it; all they need is a little tweaking.
Your best tool for debugging this is your displayMaze() routine. You just need to add two optional parameters to it to turn it into a powerful debugging tool:
void displayMaze(const vector < vector<Cell> >& arr, int curX=-1, int curY=-1)
{
.
.
.
Now if a caller omits curX & curY, the compiler fills in the defaults of -1. Now later on in that function, print a different character to indicate the "current maze position". Here's how I did it, which "seems to work" but I'm not guaranteeing it because I didn't bother to actually understand your logic:
if (curX>=0 && curY>=0 && curX==i/2 && curY==j) // ++++++++++++
cout << " * "; // * means "current position" // ++++++++++++
else if (!arr[i/2][j].truePath)
cout << " ";
else
cout << " 1 ";
Now you have a formidable debugging tool built into your program. When main() calls it without the two extra parameters, no asterisk is printed. But when you call it from solveMaze(), you can specify the "current location" so it will flag that location with '*'. In solveMaze(), add a couple variables for keeping track of the "current location"...
int x=0, y=0;
int curX=x, curY=y; // ++++++++++++++++
...then, at the top of your loop, just call your formidable debugging tool so you get the current status of your overall solution as it progresses, step-by-step:
int i=0;
while (!exitFound)
{
displayMaze(myMaze, curX, curY); // +++++++++++++++++
.
.
.
Now, wherever you change what you consider to be the "current location", just update curX and curY accordingly, and your debug tool will keep you updated so you can see the solution as it unfolds graphically (well, pseudo-graphically). You might even add debug cout messages at key logic decision points, so you can correlate those decision points with the solution you see unfolding, so you can see if/when there's a problem. And if you find a problem, you can scroll back up in your output to see where things went wrong.
Good luck!

Why am I getting this output?

Alright so I writing Conways Game of Life in C++, and so far I have only created the rule that allows users to create cells if it has 3 neighbors.
Here is the current code: http://tinypaste.com/f59b4463
When I launched the program I entered in the coordinates so that I would have the gameboard depicted in the photo below, and the output wasn't what I expected, it should have made it so that the cell 2,1 would be alive, but in the output it remained dead. I am not sure why it is not working. Any help?
Input & Output: http://i.imgur.com/1Mvhi.png
Several things to address, and while this is not an answer, it's too big for a comment. Please fix these then I will get back to you...
In gameboard() please arrange the code so that it consists of two for loops instead of all the couts. Example:
int i, j;
for (i = j = 0; i < 10; i++) {
for (; j < 10; j++) {
cout << world[i][j];
}
}
it's much more concise.
Second, in cells(), in the second for loop, you can use another nested for loop.
Third, I would avoid naming normal variables in ALL CAPS since that is generally reserved for preprocessor #defines.
K, enjoy cleaning up :)
Alright. It's an algorithmic issue. When you call calculate, it creates extra cells because it's not exactly one generation. It's a mix of two and three - it acts on cells you just created. Get what I'm saying? I explained this on GMail.