473,320 Members | 1,902 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,320 software developers and data experts.

Problem Based Passing Vectors Elements As Arguments

Hi All,

I have the following setup. Two 'std::vector's which i iterate through in a

for (iterate through vector1 of types X) {

for (iterate through vector2 of types Y) {

f(x)
}

}

loop using the vector iterators.

Ok well f(x) calls another function, and i want to pass pointers to the
elements in the two vectors that the current iterators point at to this
function.

Well i know that the iterator class is basically just a pointer to the
position in the vector anyway. However I want f(x) to remain general and
take a pointers to a type X and a type Y as arguments, rather than two
iterators.

Ok so my question is what do i pass? Is it something like
f(&(*IteratorX), &(*IteratorY))? Or is there a better way.

If my f function took references as arguments, how if at all can i set a
variable contained in X which is a pointer to type Y to the argument of
the function f which is a type Y? i.e. an element of type X in the
vector of type X's now contains a pointer to an element in the vector of
type Y's contained in the main method!

Adam
Jul 23 '05 #1
6 1728
"Adam Hartshorne" <or********@yahoo.com> wrote in message
news:d1**********@wisteria.csv.warwick.ac.uk...
....
: Ok well f(x) calls another function, and i want to pass pointers to the
: elements in the two vectors that the current iterators point at to this
: function.
:
: Well i know that the iterator class is basically just a pointer to the
: position in the vector anyway. However I want f(x) to remain general and
: take a pointers to a type X and a type Y as arguments, rather than two
: iterators.
:
: Ok so my question is what do i pass? Is it something like
: f(&(*IteratorX), &(*IteratorY))? Or is there a better way.
Yes. You can as well drop two pairs of parentheses:
f( &*IteratorX, &*IteratorY );
But if either (or both) of the function's arguments is never supposed to
be NULL, then passing references (and if possible a const&) instead of
a pointer is probably a good idea.
The calling syntax will then become:
f( *IteratorX, *IteratorY );

: If my f function took references as arguments, how if at all can i set a
: variable contained in X which is a pointer to type Y to the argument of
: the function f which is a type Y? i.e. an element of type X in the
: vector of type X's now contains a pointer to an element in the vector of
: type Y's contained in the main method!
void f( TypeX& x, TypeY& y )
{
x.field = & y; // here '&' is the address-of operator
}
Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Jul 23 '05 #2
> I have the following setup. Two 'std::vector's which i iterate through in
a

for (iterate through vector1 of types X) {

for (iterate through vector2 of types Y) {

f(x)
}

}

loop using the vector iterators.

Ok well f(x) calls another function, and i want to pass pointers to the
elements in the two vectors that the current iterators point at to this
function.

Well i know that the iterator class is basically just a pointer to the
position in the vector anyway. However I want f(x) to remain general and
take a pointers to a type X and a type Y as arguments, rather than two
iterators.

Ok so my question is what do i pass? Is it something like
f(&(*IteratorX), &(*IteratorY))? Or is there a better way.

Why not the following?
f(X&,Y&) {...}
Which you call by:
f(*IteratorX,*IteratorY);

If my f function took references as arguments, how if at all can i set a
variable contained in X which is a pointer to type Y to the argument of
the function f which is a type Y? i.e. an element of type X in the
vector of type X's now contains a pointer to an element in the vector of
type Y's contained in the main method!


Could you please explain your problem above more clearly?

Regards,
Peter
Jul 23 '05 #3
Adam Hartshorne wrote:
I have the following setup. Two 'std::vector's which i iterate through in a

for (iterate through vector1 of types X) {

for (iterate through vector2 of types Y) {

f(x)
}

}

loop using the vector iterators.

Ok well f(x) calls another function, and i want to pass pointers to the
elements in the two vectors that the current iterators point at to this
function.

Well i know that the iterator class is basically just a pointer
No, it isn't.
to the
position in the vector anyway. However I want f(x) to remain general and
take a pointers to a type X and a type Y as arguments, rather than two
iterators.
Actually, I think it would be better if you make it a template and pass
the iterators there. Pointers *are* iterators:

template<class ItX, class ItY> void f(ItX px, ItY py) {
...
// use px-> or *px here, should work for iterators (and pointers)
}

Ok so my question is what do i pass? Is it something like
f(&(*IteratorX), &(*IteratorY))? Or is there a better way.
In what way should it be better? That's the only legal way I know.
If my f function took references as arguments, how if at all can i set a
variable contained in X which is a pointer to type Y to the argument of
the function f which is a type Y? i.e. an element of type X in the
vector of type X's now contains a pointer to an element in the vector of
type Y's contained in the main method!


You should use C++ to describe the relationships between types here. So,

class X {
Y *py;
public:
void set_py(Y *py) { this->py = py; }
};

class Y {};

void f(X& rx, Y& ry) {
rx.set_py(&ry);
}

V
Jul 23 '05 #4

"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:CFi0e.55702>
class X {
Y *py;
public:
void set_py(Y *py) { this->py = py; }
};


Is that ok? I mean, you've got the same name for the parameter as the
member variable. Is the right side of that assignment guaranteed to refer
to the parameter and not the member? (Even if it is, I'd avoid the practice
and give it a different name, just to avoid confusion.)

-Howard
Jul 23 '05 #5
Howard schrieb:
"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:CFi0e.55702>
class X {
Y *py;
public:
void set_py(Y *py) { this->py = py; }
};

Is that ok? I mean, you've got the same name for the parameter as the
member variable. Is the right side of that assignment guaranteed to refer
to the parameter and not the member?


Yes, local variables and parameters shadow members. this-> can be used
to refer to the shadowed member.
(Even if it is, I'd avoid the practice
and give it a different name, just to avoid confusion.)


Indeed. For trivial cases like above I think it's acceptable though. I
always prefix members with m_ in order not to run out of names for
locals :-)

Cheers,
Malte
Jul 23 '05 #6
On Wed, 23 Mar 2005 20:01:54 +0100, Malte Starostik
<ma***@starostik.de> wrote:
Howard schrieb:
"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:CFi0e.55702>
class X {
Y *py;
public:
void set_py(Y *py) { this->py = py; }
};

Is that ok? I mean, you've got the same name for the parameter as the
member variable. Is the right side of that assignment guaranteed to refer
to the parameter and not the member?


Yes, local variables and parameters shadow members. this-> can be used
to refer to the shadowed member.


It's particularly useful for initialisers, where the only time the
member is used is to set it.
(Even if it is, I'd avoid the practice
and give it a different name, just to avoid confusion.)


Indeed. For trivial cases like above I think it's acceptable though. I
always prefix members with m_ in order not to run out of names for
locals :-)


I do the opposite, I prefix parameters with a so I write

ClassName(int aVal) : val(aVal)

It's all just a coding style convention, though, and I've worked in
places with a load of different convention on naming variables (one
where all parameters were aThing, member variables mThing, local
variables started lowercase (not followed by a lowercase letter), member
functions starting uppercase, file local functions starting lowercase
(so like local variables but followed by ( when calling them), etc.

Chris C
Jul 23 '05 #7

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

Similar topics

31
by: Brian Sabbey | last post by:
Here is a pre-PEP for what I call "suite-based keyword arguments". The mechanism described here is intended to act as a complement to thunks. Please let me know what you think. Suite-Based...
5
by: lugal | last post by:
This might be more appropriate here. I'm new to C++, coming from a background in another languages that allowed a similar solution to work (Python). I wrote the following code in C++ based on the...
33
by: abs | last post by:
Hi all. My list: <ul> <li id="a" onclick="show(this)">Aaaaaaaa</li> <li id="b" onclick="show(this)">Bbbbbbbb</li> <li id="c" onclick="show(this)">Cccccccc <ul> <li id="d"...
7
by: Harolds | last post by:
The code below worked in VS 2003 & dotnet framework 1.1 but now in VS 2005 the pmID is evaluated to "" instead of what the value is set to: .... xmlItems.Document = pmXML // Add the pmID...
39
by: Martin Jørgensen | last post by:
Hi, I'm relatively new with C-programming and even though I've read about pointers and arrays many times, it's a topic that is a little confusing to me - at least at this moment: ---- 1)...
2
by: danielhdez14142 | last post by:
Some time ago, I had a segment of code like vector<vector<int example; f(example); and inside f, I defined vector<int>'s and used push_back to get them inside example. I got a segmentation...
2
by: sorobor | last post by:
dear sir .. i am using cakephp freamwork ..By the way i m begener in php and javascript .. My probs r bellow I made a javascript calender ..there is a close button ..when i press close button...
17
by: Matt | last post by:
Hello. I'm having a very strange problem that I would like ot check with you guys. Basically whenever I insert the following line into my programme to output the arguments being passed to the...
5
by: arnuld | last post by:
/* C++ Primer 4/e * STATEMENT * given 2 vectors of integers, write a programme to determine whether one vector * is the prefix of the other vector e.g. if 1st vector has elements...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.