C Board  

Go Back   C Board > General Programming Boards > C++ Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 11-04-2009, 12:19 PM   #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
cardinals03 is offline   Reply With Quote
Old 11-04-2009, 12:22 PM   #2
Registered User
 
Join Date: Sep 2004
Location: California
Posts: 2,845
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.

Quote:
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.
bithub is offline   Reply With Quote
Old 11-04-2009, 12:31 PM   #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???
cardinals03 is offline   Reply With Quote
Old 11-04-2009, 01:09 PM   #4
Mysterious C++ User
 
Join Date: Oct 2007
Posts: 14,099
What type is the list?
std::list's iterators are incrementable for some reason, so that code would fail if that was the case.
__________________
Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2008 Team System
I dedicated my life to helping others. This is only a small sample of what they said:
"Thanks Elysia. You're a programming master! How the hell do you know every thing?"
Quoted... at least once.
Quote:
Originally Posted by cpjust
If C++ is 2 steps forward from C, then I'd say Java is 1 step forward and 2 steps back.
Elysia is offline   Reply With Quote
Old 11-04-2009, 02:07 PM   #5
Registered User
 
Join Date: Jan 2005
Posts: 7,137
>> 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.
Daved is offline   Reply With Quote
Old 11-04-2009, 02:12 PM   #6
Mysterious C++ User
 
Join Date: Oct 2007
Posts: 14,099
"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.
__________________
Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2008 Team System
I dedicated my life to helping others. This is only a small sample of what they said:
"Thanks Elysia. You're a programming master! How the hell do you know every thing?"
Quoted... at least once.
Quote:
Originally Posted by cpjust
If C++ is 2 steps forward from C, then I'd say Java is 1 step forward and 2 steps back.
Elysia is offline   Reply With Quote
Old 11-04-2009, 02:29 PM   #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....
cardinals03 is offline   Reply With Quote
Old 11-05-2009, 10:34 AM   #8
Registered User
 
Join Date: Apr 2006
Posts: 1,193
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.
King Mir is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
sorting number Leslie C Programming 8 05-20-2009 04:23 AM
Easy pointer question Edo C++ Programming 3 01-19-2009 10:54 AM
char pointer to pointer question Salt Shaker C Programming 3 01-10-2009 11:59 AM
Pointer question rakan C++ Programming 2 11-19-2006 02:23 AM
pointers InvariantLoop C Programming 13 02-04-2005 09:32 AM


All times are GMT -6. The time now is 07:51 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22