473,396 Members | 2,010 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

strange behavior of vector template iterator

nicolaennio
I am new with c++ and I cannot understand why this code:

Expand|Select|Wrap|Line Numbers
  1. #include <cstdlib>
  2. #include <iostream>
  3. #include <vector>
  4.  
  5. using namespace std;
  6.  
  7. vector<int>::iterator _begin(vector<int> v){
  8.     return v.begin();
  9. }
  10.  
  11. int main(int argc, char *argv[])
  12. {
  13.     vector<int> v;
  14.     v.push_back(4);
  15.     vector<int>::iterator b = _begin(v);
  16.     printf("%d\n",*b);
  17.     printf("%d\n",*_begin(v));
  18.  
  19.     system("PAUSE");
  20.     return EXIT_SUCCESS;
  21. }
  22.  
gives me this output:
0
4

thanks
Feb 24 '07 #1
4 1337
willakawill
1,646 1GB
Well I get -17890612 for both. And this is not beginner stuff :)
Feb 24 '07 #2
thanx for trying it out, actually I am not a beginner in programming, but in c++ yes and I have some difficulties.
I tried it at home and I get something like
40546540
4
Since code is small I could code around this problem, but I need using vector rather than stack and hope it is a problem like "say to the compiler what template you use and ... (it will say who you are :-) )".
It is strange that in your case neither printf worked
Feb 24 '07 #3
ok, I find out the problem, since vector I pass in input is by value a copy is made
in the function and iterator work on the copy, so I return a useless iterator.
I don't know why on my machine it printed 0 rather than an impossible value as willakawill.
Feb 25 '07 #4
willakawill
1,646 1GB
Excellent work. Glad you found it
Feb 25 '07 #5

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

Similar topics

7
by: zhou | last post by:
Hi there, We have a compiler specific issue which requires us to force template instantiation. This works fine. The problem comes when I try using std:find() on vector. Since vector has no member...
2
by: Dave | last post by:
I'm crossposting this to both comp.lang.c++ and gnu.gcc because I'm not sure if this is correct behavior or not, and I'm using the gcc STL and compiler. When calling vector<int>::push_back(0),...
17
by: Michael Hopkins | last post by:
Hi all I want to create a std::vector that goes from 1 to n instead of 0 to n-1. The only change this will have is in loops and when the vector returns positions of elements etc. I am calling...
4
by: Generic Usenet Account | last post by:
I am seeing some unexpected behavior while using the STL "includes" algorithm. I am not sure if this is a bug with the template header files in our STL distribution, or if this is how it should...
9
by: aaragon | last post by:
I am trying to create a vector of type T and everything goes fine until I try to iterate over it. For some reason, the compiler gives me an error when I declare std::vector<T>::iterator iter;...
24
by: toton | last post by:
Hi, I want to have a vector like class with some additional functionality (cosmetic one). So can I inherit a vector class to add the addition function like, CorresVector : public...
10
by: desktop | last post by:
I have read about input, output forward etc iterators. But if I make the following: int myints = {1,2,3,4,5,1,2,3,4,5}; std::vector<intmyvector (myints,myints+10); std::vector<int>::iterator...
5
by: Phil Endecott | last post by:
Dear All, For some reason I had got it into my head that std::vector<T>::iterator and std::basic_string<T>::iterator were certain to be T*. I now see that this isn't true. So here's why this...
8
by: Bo Yang | last post by:
Hi, Today, I make some test on the C++ STL iterators of set containers with GNU g++ compiler. The code is: #include <set> #include <iostream> using namespace std; int main(int argc, char...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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,...
0
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...

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.