473,396 Members | 1,785 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.

get vector from iterator

Hi all,

Is there any way to get a pointer to a vector (or the vector itself) if you
only have an iterator of it? Something like this:

DoSomething(vector<double>::iterator x)
{
vector<double> *q;
//do something to x so that this works
q=x.GiveMeAPointerToTheVector(please);
}

And another, somewhat related question:

The basics:

class Test
{
public:
double d;
void DoSomething(double t);
};

Now in another file I do:

vector<Test> x;
than I push_back lots of stuff into x and at one point do

x.DoSomething(double t);

Now in the function DoSomething, can I use 'this' in a nifty way to get a
pointer to the vector x?
Something like
void DoSomething(double t)
{
vector<Test> *q;
q=this->SomeNiftyThings();
}

Thanks for any help!

Jens

Jul 23 '05 #1
6 1599
Dr. J.K. Becker wrote:
Is there any way to get a pointer to a vector (or the vector itself) if you
only have an iterator of it?
Not portably, no. Why do you need it? If you need a vector, just pass
the vector into your function, you can always get an iterator from it.
[...]
And another, somewhat related question:

The basics:

class Test
{
public:
double d;
void DoSomething(double t);
};

Now in another file I do:

vector<Test> x;
than I push_back lots of stuff into x and at one point do

x.DoSomething(double t);
This is a syntax error, first of all. Second, 'x' is a 'vector'.
'vector' does not have member 'DoSomething'. Do you mean you do

x[someindex].DoSomething(somedoublevalue);

?
Now in the function DoSomething, can I use 'this' in a nifty way to get a
pointer to the vector x?
No. A contained object does not know it is contained anywhere *unless*
you somehow tell the object where it is contained.
[...]


V
Jul 23 '05 #2

Dr. J.K. Becker wrote:
Hi all,

Is there any way to get a pointer to a vector (or the vector itself) if you only have an iterator of it?


No. An iterator is like a pointer and you can't retrieve the
originating container from it according to my experience and all
documentation I can find.

Jul 23 '05 #3
Dr. J.K. Becker wrote:
Is there any way to get a pointer to a vector (or the vector itself) if you only have an iterator of it?


No.
--
<mailto:di***********@yahoo.com> <http://www.dietmar-kuehl.de/>
<http://www.contendix.com> - Software Development & Consulting

Jul 23 '05 #4
Hmmm,

Well, at least you all have the same short answer :-). Oh well, I have to
figure out something else then. Thanks for the help!

Jens

Jul 23 '05 #5

Dr. J.K. Becker wrote:
Now in the function DoSomething, can I use 'this' in a nifty way to get a pointer to the vector x?
Something like
void DoSomething(double t)
{
vector<Test> *q;
q=this->SomeNiftyThings();
}


With some forward planning:

class Content
{
std::vector<Content&> *container;
public:
Content& set_container(std::vector<Content&> vector)
{
container = &vector;
return *this;
}
};

example use:
std::vector<Content&> x;
Content y;

x.push_back(y.set_container(x));

If you are not holding references but instead copies I think this would
be a little more fun. Also keep in mind that adding the same reference
to different containers breaks that code. You would have to do a lot
more planning than that code represents. That is just a general idea
of you could approach the problem. You could probably use templates
somehow to generalize the approach but it won't be too terribly easy.

If you really need this functionality then do it; but you probably have
other options you have not thought of. I have never needed
functionality like what you are trying to do.

Jul 23 '05 #6
Noah Roberts wrote:
[...]
class Content
{
std::vector<Content&> *container;
There are no containers of references.
[...]

Jul 23 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: Jim West | last post by:
I'm curious why I might be getting such a large performance difference between using a map iterator and a vector iterator. This is a computational electromagnetics code where I have to separate...
18
by: Janina Kramer | last post by:
hi ng, i'm working on a multiplayer game for a variable number of players and on the client side, i'm using a std::vector<CPlayer> to store informatik about the players. CPlayer is a class that...
29
by: Hagen | last post by:
Hello, in a recent thread "speed of vector vs array" I read about the problem of the slow acces by addressing vector elements by indexing, unfortunately I see no workaround in my case. My...
11
by: koperenkogel | last post by:
Dear cpp-ians, I am working with a vector of structures. vector <meta_segment> meta_segm (2421500); and the structure look like: struct meta_segment { float id; float num;
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...
3
by: codefixer | last post by:
Hello, I am trying to understand if ITERATORS are tied to CONTAINERS. I know the difference between 5 different or 6(Trivial, on SGI). But what I fail to understand is how can I declare all 5...
9
by: Amadeus W. M. | last post by:
I have a vector from which I want to erase elements between iterators i,j. If i<j, everything works as expected, but if j>i, an insertion is actually performed. Example: vector<double> x(10);...
4
by: arnuld | last post by:
i wrote a programme to create a vector of 5 elements (0 to 4), here is the code & output: #include <iostream> #include <vector> int main() { std::vector<intivec; // dynamically create a...
1
by: atomik.fungus | last post by:
Hi, I'm re-writting my matrix class to practice my programming and the computer doesn't let me compile the next code: ( this example come from the constructor of the class) //the matrix is made...
12
by: desktop | last post by:
Why does insert only work when specifying an iterator plus the object to be inserted: std::vector<intt; std::vector<int>::iterator it; it = t.begin(); t.insert(it,33); If I use push_back...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
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
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,...

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.