CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Jun 2015
    Posts
    175

    Using containers list and vector in a program

    Hi all,

    Here's a code using a list of vectors to read a text terminated by ctrl+z and then print it all in the same order. The problem is that I don't know why is the function Text_iterator& Text_iterator:: operator++() correct and especially how can the condition if (pos == ln->end()) be true!?

    Code:
    #include <iostream>
    #include <vector>
    #include <list>
    
    using Line = std::vector<char>;
    using listIter = std::list<Line>::iterator;
    using lineIter = Line::iterator;
    
    struct Text_iterator {
    
    	listIter ln;
    	lineIter pos; 
    
    	Text_iterator(listIter ll, lineIter pp)
    		: ln{ ll }, pos{ pp } { }
    
    	char& operator*() { return *pos; }
    
    	Text_iterator& operator++();
    
    	bool operator==(const Text_iterator& other) const
    	{
    		return ln == other.ln && pos == other.pos;
    	}
    	bool operator!=(const Text_iterator& other) const
    	{
    		return !(*this == other);
    	}
    };
    
    //*****************************
    
    Text_iterator& Text_iterator::operator++()
    {
    	++pos;    
       if (pos == ln->end()) {
    		++ln;  // "ln" pointing to the next vector/line
    		pos = ln->begin();
    	} 
    	return *this;
    }
    
    //************************
    
    class Document {
    public:
    	std::list<Line> lines;
    
    	Document() { lines.push_back(Line{}); } 
    
    	Text_iterator begin()
    	{
    		return Text_iterator(lines.begin(), lines.begin()->begin());
    	}
    
    	Text_iterator end()
    	{
    		listIter last = lines.end();
    		--last;  // We know that the document is not empty
    		return Text_iterator(last, last->end());
    	}
    };
    
    //******************************************************
    
    std::istream& operator>>(std::istream& is, Document& d)
    {
    	char ch;
    	while (is.get(ch)) 
    	{
    		d.lines.back().push_back(ch); 
    		if (ch == '\n')
    			d.lines.push_back(Line{}); // An empty vector of char with uninitialized values
    	}
    
    	if (d.lines.back().size()) d.lines.push_back(Line{});  // Add final empty line
    	return is;
    }
    
    //******************************
    
    int main()
    {
    	Document d;
    
    	std::cout << "Please enter characters for your list. Type ^z at the end.\n";
    	std::cin >> d;
    
    	for (auto p = d.begin(); p != d.end(); ++p)
    		std::cout << *p;
    
    	system("pause");
    	return 0;
    }

  2. #2
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Using containers list and vector in a program

    Use the debugger to trace through the code to see what is happening and the contents of the variables. Then you'll see exactly.

    Code:
    if (pos == ln->end())
    pos starts at ln->begin(). ln->end() points one past the end - as per convention for an iterator. Hence if pos == ln->end(), pos has been incremented past the last position and is reset to ln->begin() with ln being incremented.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  3. #3
    Join Date
    Jun 2015
    Posts
    175

    Re: Using containers list and vector in a program

    Ow ****, ln->begin() equals lines.begin()->begin() that is, it pointes to the first element (char) of the vector, and ln->end() points to one beyond the last element in the current vector not the list! My bad. I spent much time for that but couldn't trace such a simple thing. F..k me.

    Thank you so much for the help. I tried to use the debugger by running the code using F10 & F11 but couldn't take much benefit from it. Did you find the issue that way? How please? That will be very helpful for forthcoming projects.

  4. #4
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Using containers list and vector in a program

    Did you find the issue that way?
    Nope - I just looked at the code.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  5. #5
    Join Date
    Jun 2015
    Posts
    175

    Re: Using containers list and vector in a program

    And by using the debugger the intention is to run and follow the control in the code by F10 & F11 in VS, right?

    I'm sorry that I can't give you a Rate! That system gives me a message to spread some around! I hope that bug fixes shortly.

  6. #6
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Using containers list and vector in a program

    And by using the debugger the intention is to run and follow the control in the code by F10 & F11 in VS, right?
    Yes - and to watch the contents of the variables. Not only do you get to understand the code, you also get to debug it to make sure it's working as expected and to find out why if it's not. Anyone coding in c++ needs to be able to use the debugger to debug code. It's not really optional. Over the years I've worn off the F10 F11 legends on about 4 keyboards.....


    I'm sorry that I can't give you a Rate! That system gives me a message to spread some around! I hope that bug fixes shortly.
    Don't sweat it. Pleased to help. It's not a bug - it's a 'feature' to stop one person repeatedly rating the same person to potentially inflate their rating. It's something like having to rate at least 5 (or is it 3?) others before you can rate the same person again.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  7. #7
    Join Date
    Jun 2015
    Posts
    175

    Re: Using containers list and vector in a program

    Yes - and to watch the contents of the variables.
    Is the window called Autos in the screenshot below where I ought to focus on and should I then expand the elements, e.g., d, d.line. is, here, for more concentration? I mean is it the proper method of using the debugger?

    Attachment 35942

    I see your remarks about that rating feature but what if you help me with a couple of questions/posts that solve my issues? The logical approach is to be able to give you a rate per post/thread if that's helpful. Still I feel you should have been given much more rates and I regret that it's not feasible.
    Anyway, thanks.

  8. #8
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Using containers list and vector in a program

    Quote Originally Posted by tomy12 View Post
    And by using the debugger the intention is to run and follow the control in the code by F10 & F11 in VS, right?

    I'm sorry that I can't give you a Rate! That system gives me a message to spread some around! I hope that bug fixes shortly.
    You'll need to also use F9 to set a breakpoint before using F10 & F11. Otherwise, F10 & F11 aren't much use.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured