473,769 Members | 6,473 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

help please with list<string> iterator problem

4 New Member
I'm working my way through the accelerated C++ book, and exercise 10-5, & 10-6 have me stuck

I have the follwing class in a header file:
Expand|Select|Wrap|Line Numbers
  1. class String_list
  2. {
  3. public:
  4.     String_list();
  5.     void addString (const std::string s);
  6.     void print_strings () const;
  7.     std::string* biIterator(int dx);
  8. private:
  9.     std::string _dummy;
  10.     std::list<std::string> _strings;
  11.     std::list<std::string>::const_iterator _iter;
  12. };
  13.  
  14. and its functions in a cpp file:
  15.  
  16. #include "String_list.h"
  17.  
  18. String_list::String_list()
  19. {
  20.     _iter = _strings.begin();
  21. }
  22.  
  23. void String_list::addString(const string s)
  24. {
  25.     _strings.push_back(s);    
  26. }
  27.  
  28. void String_list::print_strings () const
  29. {
  30.     list<string>::const_iterator it;
  31.     for(it = _strings.begin();it!=_strings.end();++it)
  32.         cout << *it << endl;
  33. }
  34.  
  35.  
  36. string* String_list::biIterator(int dx)
  37. {
  38.     //assigning _iter here to begin works, yet initializing in constructor doesnt
  39.     //_iter = _strings.begin();
  40.     if (dx>0){
  41.         for(int count = 0;count!=dx;++count)
  42.             ++_iter;
  43.     }else{
  44.         for(int count = dx;count!=0;--count)
  45.             --_iter;
  46.     }
  47.     cout << *_iter << endl;
  48.     _dummy = *_iter;
  49.     string* pS = &_dummy;
  50.     return pS;
  51. }
  52.  
All compiles OK, but I get a runtime error "list iterator not incrementable"

The bug goes away if instead of assigning _iter in the constructor I assign it in the biIterator function. Assigning _iter in the addString function gives the same error. I understand why I cant initialize it in the constructor, but why not in the addString func ? what is going on, can someone please help ?
Aug 17 '07 #1
1 3770
cakeathon
4 New Member
I think I have found the bug after several hours of head scratching,

I created my String_list using a function that called addString and pass a copy back to main. In passing the copy back to main the iterator to begin is no longer valid.
Aug 17 '07 #2

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

Similar topics

2
2528
by: gbgbgbgb | last post by:
Hi, I have a definition bool operator<(string s_s, string s_t) { .... } and a variable list<string> concomp;
2
5076
by: s | last post by:
I'm getting compile errors on the following code: <code> #include <iostream> #include <fstream> #include <list> #include <string> using namespace std;
3
8494
by: aquanutz | last post by:
Ok, I have a list of strings (list<string> stringList) that I want to sort alphabetcially, only "sort(stringList.begin(), stringList.end()); ) does not work. Any insight would be helpful. Thanks!
3
15182
by: Abhi | last post by:
In the following hypothetical example I want to build a generic list of unique string items. How should I implement the pred function so that it returns true/false if target string exists in the generic list...is not clear to me. Any suggestions? System.Collections.Generic.List<string> list = new System.Collections.Generic.List<string>(); foreach(string s in myAnotherArray)
6
46188
by: buzzweetman | last post by:
Many times I have a Dictionary<string, SomeTypeand need to get the list of keys out of it as a List<string>, to pass to a another method that expects a List<string>. I often do the following: <BEGIN CODE> List<stringkeyNameList = new List<string>(); foreach (string keyName in this.myDictionary.Keys)
4
12246
by: Maikeru | last post by:
I am trying to create a map that contains a string as the key and a list of strings as the value. Essentially it will be a dictionary where the key is an English word and the list will contain all Latin equivalents. My program reads from a file containing Latin words and their English equivalents. I am using the operator from map so that I can add to the list<string> should the Key already exist in my map. I am not sure of the syntax that I need...
2
5895
by: Assimalyst | last post by:
Hi I have a Dictionary<string, List<string>>, which i have successfully filled. My problem is I need to create a filter expression using all possible permutations of its contents. i.e. the dictionary essentially creates the following array: Key Value
4
3954
by: parez | last post by:
Hi, I am trying to serialize List<List<string>. With the following code public List<List<string>DataRows { get; set; }
3
3453
by: banangroda | last post by:
Compilation fails at "line.insert(line.end(), x.begin(), i);" and I can't figure out why. Here is the code: /* 5-1. Design and implement a program to produce a permuted index. A permuted index is one in which each phrase is indexed by every word in the phrase. */
0
9589
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10049
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9997
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8873
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7413
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6675
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5448
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3565
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.