Hi.
I am new to c++ and programing in general and have run into a problem on one of my assignments. I am trying to insert one vector, into a certain spot in another vector. Both are of the same type <string>. My main problem is we have not really learned iterators, and my previous code sort of works around them. Is it possible to do this without iterators or what would be the easiest way to implement them into my code.

Sorry it this is not very clean. I used dev-c++ 4.9.9.2 to compile.

mMap is a mutli map where some keys have multiple elements to them
randIntInc is a sort of ghetto way of choosing a random element from each key.
sentence and terminalInsert are both vector<string>

getTokens(string x) is a function which parses a string(with spaces and multiple words) into a vector with 1 word per element. If you need to see the function just let me know, but I believe i found it on this site.

for(int j=0;j< sentence.size();j++)
    {if (sentence[j][0]='<')    // Checks to see if element in vector
                                // is non terminal
     terminalInsert.clear();    //clears temporay vector used to insert
     key=sentence[j];        //sets key to non terminal value form vector
     itr1 = mMap.find(key); //sets multimap iterator to first key
     randIntInc=rand()%(mMap.count(key));   //randomly choose element
        for(int n=0; n<randIntInc; n++)     //with key=key
         {itr1++;}                          
     terminalInsert=getTokens(itr1->second)//parses element from map
                                           //into string vector 
                                           //for inserting into sentence

Thanks for takinga look and I apologize for how sloppy it is. If theres any questions let me know what else you need to know.

Recommended Answers

All 4 Replies

Without looking at the code... I must ask... is it truly required for both vectors to be of the same type ( <string> ) to complete the assignment?

Your if statement will almost always evaluate to be true because you're using the single = (assignment) operator as opposed to the == (equals) operator. Is this really what you want to do? I haven't seen all of your code so it is hard to tell @_@

No wow O_O that was definiately a mistake let me see what I come up with after fixing that. Haha thanks for spotting that; i feel so dumb.

edit: ok I fixed that but the problem is I want to do something like

for (int m=0; m<terminalInsert.size(); m++)
{sentence.insert(j, terminalInsert[m]);}

but I guess insert will only take a iterator as the first parameter. Would a solution to be just to run an iterator "side by side" with my code doing nothing really until I get to this point?

Hmm my program is now crashing when I try to run it ;/

I uploaded my entire source, the file used for testing, and a screenshot of my test before it crashes.

If anyone knows what im doing wrong or wants me to explain what im trying to do with this program just let me know

Better explain what problems you have with iterators. Do you want to invent quadrate wheels?..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.