473,385 Members | 1,615 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

any dangers to storing map iterator in vector?

131 128KB
I'm trying out the code below; it runs and produces the output I'd expect, but could it lead to undefined behavior?

To test with, I show the key and wait for user input; if the input doesn't match the value, the iterator is stored in the vector.

Expand|Select|Wrap|Line Numbers
  1. map<string, string> test;
  2. map<string, string>::iterator it;
  3.  
  4. vector<map<string, string>::iterator> bad_input;
  5.  
  6. string input;
  7.  
  8. test["key 1"] = "value 1";
  9. test["key 2"] = "value 2";
  10. test["key 3"] = "value 3";
  11.  
  12. for(it = test.begin(); it != test.end(); ++it)
  13. {
  14.    cout << it->first;
  15.    cin >> input;
  16.  
  17.    if(it->second != input)
  18.        bad_input.push_back(it);
  19. }
  20.  
  21. for(unsigned i=0; i < bad_input.size(); ++i)
  22.     cout << bad_input[i]->first
  23.          << "   "
  24.          << bad_input[i]->second
  25.          << "\n";
  26.  
I am also interested in knowing if I can use an iterator to walk through the vector? I tried

Expand|Select|Wrap|Line Numbers
  1. vector<map<string, string>::iterator>::iterator v_it;
  2.  
  3. for(v_it = bad_input.begin(); v_it != bad_input.end(); ++v_it)
  4.     cout << v_it-> ??
  5.  
However, I couldn't figure out how to access "first" and "second" using this method. Is it possible to do so?

TIA
Jan 10 '13 #1

✓ answered by Banfa

There is nothing inherently wrong in storing a map iterator in a string but remember that if the item is deleted from the map the iterator becomes invalid so it is probably not something you would want to keep stored long term but temporarily storing it during the execution of a single function would probably be OK.

However my own approach to such a problem would be to store the key of the item of interest in a vector. Yes this may be a little less efficient but it does not suffer from the issue of the iterator possibly becoming invalid, if the item is deleted from the map then when you read your key from your vector and try to use it to access your map you get an error that can be dealt with rather than some form of undefined behaviour.

However if you go ahead and store an iterator to a map in a vector and then you would use the vector iterator like this

Expand|Select|Wrap|Line Numbers
  1. vector<map<string, string>::iterator>::iterator v_it;
  2.  
  3. // Point the iterator somewhere
  4.  
  5. (*v_it)->first; // * derefence the vector iterator into the type of the vector, map iterator 
  6.                 // then -> access members of the map iterator
  7.  

2 4271
Banfa
9,065 Expert Mod 8TB
There is nothing inherently wrong in storing a map iterator in a string but remember that if the item is deleted from the map the iterator becomes invalid so it is probably not something you would want to keep stored long term but temporarily storing it during the execution of a single function would probably be OK.

However my own approach to such a problem would be to store the key of the item of interest in a vector. Yes this may be a little less efficient but it does not suffer from the issue of the iterator possibly becoming invalid, if the item is deleted from the map then when you read your key from your vector and try to use it to access your map you get an error that can be dealt with rather than some form of undefined behaviour.

However if you go ahead and store an iterator to a map in a vector and then you would use the vector iterator like this

Expand|Select|Wrap|Line Numbers
  1. vector<map<string, string>::iterator>::iterator v_it;
  2.  
  3. // Point the iterator somewhere
  4.  
  5. (*v_it)->first; // * derefence the vector iterator into the type of the vector, map iterator 
  6.                 // then -> access members of the map iterator
  7.  
Jan 10 '13 #2
divideby0
131 128KB
Banfa, thank you for your time and explanation; as always, I appreciate it.
Jan 11 '13 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: zhou | last post by:
Hi there, We have a compiler specific issue which requires us to force template instantiation. This works fine. The problem comes when I try using std:find() on vector. Since vector has no member...
29
by: Hagen | last post by:
Hello, in a recent thread "speed of vector vs array" I read about the problem of the slow acces by addressing vector elements by indexing, unfortunately I see no workaround in my case. My...
4
by: Arturo Cuebas | last post by:
I've got a bunch of file_iterator<> (http://tinyurl.com/3uuxa) begin/end pairs that point to various chunks in a file. It would be super-cool, in my program, to be able to treat all of these ranges...
11
by: koperenkogel | last post by:
Dear cpp-ians, I am working with a vector of structures. vector <meta_segment> meta_segm (2421500); and the structure look like: struct meta_segment { float id; float num;
4
by: steflhermitte | last post by:
Dear cpp-ians, I am working with a structure struct segment { .... vector <meta_segment>::iterator it_Z; .... };
9
by: Manuel | last post by:
Hi. I'm a question about vectors. if I write a code that (in pseudo code): 1)Declare vector1 2)Declare vector2 3)vector1.push_back(vector2) 4)... 5)vector2 filled with various elements
24
by: toton | last post by:
Hi, I want to have a vector like class with some additional functionality (cosmetic one). So can I inherit a vector class to add the addition function like, CorresVector : public...
5
by: toton | last post by:
Hi, I have a deque of Point class,. Point class have two fields x, and y. Now I want another class Character should point to a portion of the deque, and allow iteration only on the portion. Thus...
1
by: alan.f22 | last post by:
HI First off, thanks for taking the time to read this. I have an class inheritance hierachy say: base class "E" and a bunch of derived classes (from "E") "A", "P", and "S" I have an STL...
4
by: alexjcollins | last post by:
The following program demonstrates the problem: #include <vector> #include <iostream> #include <tvmet/Vector.h> typedef tvmet::Vector<double, 3Vector3d; class Mesh {
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.