473,769 Members | 1,743 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1751
"Adam Hartshorne" <or********@yah oo.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,*I teratorY);

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.********@com Acast.net> wrote in message
news:CFi0e.5570 2>
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.********@com Acast.net> wrote in message
news:CFi0e.5570 2>
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***@starosti k.de> wrote:
Howard schrieb:
"Victor Bazarov" <v.********@com Acast.net> wrote in message
news:CFi0e.5570 2>
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
2440
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 Keyword Arguments ----------------------------- Passing complicated arguments to functions is currently awkward in Python. For example, the typical way to define a class property winds up polluting the class's namespace with the property's get/set...
5
9017
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 Python code found here: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/302478 //beginning #include <vector>
33
2867
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" onclick="show(this)">111111</li>
7
2868
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 parameter to the XSLT stylesheet XsltArgumentList xsltArgList = new XsltArgumentList(); xsltArgList.AddParam("pmID", "", pmID); xmlItems.TransformArgumentList = xsltArgList;
39
19647
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) What's the difference between these 3 statements: (i) memcpy(&b, &KoefD, n); // this works somewhere in my code
2
3324
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 fault which I resolved by doing vector<vector<int example; example.push_back(vector<int>());
2
3156
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 then the calender gone actually i want if i click outside off the calender then it should me removed ..How kan i do this ... Pls inform me as early as possible .. I am waiting for ur quick replay ...Here i attached the source code .... <!DOCTYPE...
17
3379
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 programme: printf("\nCommand line arguement %d: %s.", i , argv ); The porgramme outputs 3 of the command line arguements, then gives a segmentation fault on the next line, followed by other strange
5
1616
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,1,1,2) and 2nd * vecotr has elements (0,1,1,2,3,5,8) then programme should say "TRUE" and if 2nd * vector is smaller then too it should say "TRUE", else it should say "FALSE". * */
0
9423
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10047
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9995
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9863
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8872
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6674
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5304
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3962
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3563
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.