![]() |
| | #1 |
| Registered User Join Date: Oct 2009
Posts: 10
| pointer 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; |
| cardinals03 is offline | |
| | #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:
__________________ bit∙hub [bit-huhb] n. A source and destination for information. | |
| bithub is offline | |
| | #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 | |
| | #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:
| |
| Elysia is offline | |
| | #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 | |
| | #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;
}
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:
| |
| Elysia is offline | |
| | #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 | |
| | #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 | |
![]() |
| Thread Tools | |
| Display Modes | |
|
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 |