Thread: pointer question

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    10

    pointer question

    I have the following review question.

    A list contains the values 30,40,60 in that order.
    p&q are iterators that point to integers on the list.

    What will the following print?

    Code:
    p = intlist.begin();
    p++;
    q = intlist.insert(p, 20);
    p = intlist.begin();
    p++
    cout << *p + *q;
    I think it is 40. p initially starts at 30 then increments to 40. q then inserts 20 as the value. p then goes back to the beginning of the list (30) and increments to the next value (now 20). Am I right? The thing that is hanging me up is wondering whether or not the value of p is updated by p++ or if it stays 30 (then the output would be 50). Thanks

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    It would take you about 20 seconds to write a program that would answer your question for you. That's probably less time than it took you to write up this question in the first place.

    wondering whether or not the value of p is updated by p++ or if it stays 30
    p++ moves the iterator to the next position in the list.
    bit∙hub [bit-huhb] n. A source and destination for information.

  3. #3
    Registered User
    Join Date
    Oct 2009
    Posts
    10
    Thanks for the asinine reply---I am away from my compiler and was looking over a set of posted review problems. Guess how long it took me to type this???

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    What type is the list?
    std::list's iterators are incrementable for some reason, so that code would fail if that was the case.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> std::list's iterators are incrementable for some reason, so that code would fail if that was the case.

    Did you mean "aren't" instead of "are"? If yes, then I'm not sure why you think that. They are incrementable. If not, then your statement doesn't make sense.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    "are not"*, is what I meant. But your reply got me thinking. I would expect they should incrementable and the reason I said they were not was because I got an error saying list iterators are not incrementable. But it seems the problem was on my part--I did not actually put any data into the list.
    So without further ado, this code:
    Code:
    #include <list>
    #include <iostream>
    
    int main()
    {
    	std::list<int> intlist;
    	std::list<int>::iterator p, q;
    	intlist.push_back(30);
    	intlist.push_back(40);
    	intlist.push_back(60);
    	p = intlist.begin();
    	p++;
    	q = intlist.insert(p, 20);
    	p = intlist.begin();
    	p++;
    	std::cout << *p + *q << std::endl;
    }
    Produces the output: 40.
    So there is your answer.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Oct 2009
    Posts
    10
    Elysia,
    Thanks for your reply. I am stuck in airport right now and was just looking over some review material. I know this was an easy fix. However, I hate it when something gets stuck in my head (the easy things that our brains make hard). Thanks again....

  8. #8
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Next time use codepad.org.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. Easy pointer question
    By Edo in forum C++ Programming
    Replies: 3
    Last Post: 01-19-2009, 10:54 AM
  3. char pointer to pointer question
    By Salt Shaker in forum C Programming
    Replies: 3
    Last Post: 01-10-2009, 11:59 AM
  4. Pointer question
    By rakan in forum C++ Programming
    Replies: 2
    Last Post: 11-19-2006, 02:23 AM
  5. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM