Board index » cppbuilder » How to pass a reference to a std::vector in a function.

How to pass a reference to a std::vector in a function.


2006-01-31 12:54:37 AM
cppbuilder6
I have 3 vectors std::vector<std::string>. I'd like to pass one by
address or referenct in a function call and use it. How do I do
this?
Thanks
Nate
 
 

Re:How to pass a reference to a std::vector in a function.

"RNLockwood" < XXXX@XXXXX.COM >writes:
Quote
I have 3 vectors std::vector<std::string>. I'd like to pass one by
address or referenct in a function call and use it. How do I do
this?
It's no different from passing any other variable type by reference to
a function. Why don't you show us the code you tried, explain what
the problem was with what you had, and then we'll help you fix it.
For an int;
void f(int & i)
{
// use i
}
int main()
{
int x = 999;
f(x);
}
For a vector instantiation, replace int with your type.
--
Chris (TeamB);
 

Re:How to pass a reference to a std::vector in a function.

Nate Lockwood wrote:
Quote
BTW I think that the NTTP web based interface is not accepting
posts...
Then how could Chris reply to yours?
--
Rudy Velthuis [TeamB] rvelthuis.de/
"Having the source code is the difference between buying a house and
renting an apartment." -- Behlendorf
 

{smallsort}

Re:How to pass a reference to a std::vector in a function.

Chris Uzdavinis (TeamB) wrote:
Quote
It's no different from passing any other variable type by reference to
a function.
Well, my face is red! There was a typo that caused another error that I
interpreted incorrectly. I did have it right all along.
BTW I think that the NTTP web based interface is not accepting posts...
Thanks
Nate
 

Re:How to pass a reference to a std::vector in a function.

Rudy Velthuis [TeamB] wrote:
Quote
Nate Lockwood wrote:

>BTW I think that the NTTP web based interface is not accepting
>posts...

Then how could Chris reply to yours?

Ahh Ha, you don't see it either, Rudy, so it's not just my reader
(Thunderbird). :-)
The one I sent and doesn't show was a reply to Chris. I sent another to
different group and it doesn't display either. The US Department of
Agriculture has closed the newsgroup ports so all I have at work is the
net interface.
Nate
 

Re:How to pass a reference to a std::vector in a function.

Nate Lockwood wrote:
Quote
Well, my face is red! There was a typo that caused another error
that I interpreted incorrectly. I did have it right all along.
Always use a typedef. I think in three or four years of using the STL I
once came across a situation where a typedef wasn't needed for clarity
:)
typdef std::vector<std::string>TMyStringList;
void wibble( TMyString & ListOfStrings );
so much easier than
void wibble( std::vector<std::string>& ListOfStrings );
:)
--
Andrue Cope [TeamB]
[Bicester, Uk]
info.borland.com/newsgroups/guide.html
 

Re:How to pass a reference to a std::vector in a function.

At 04:32:42, 31.01.2006, Nate Lockwood wrote:
Quote
The one I sent and doesn't show was a reply to Chris. I sent another
to different group and it doesn't display either. The US Department of
Agriculture has closed the newsgroup ports so all I have at work is the
net interface.
I see a few of yours in off-topic, and a few here.
--
Rudy Velthuis [TeamB] rvelthuis.de/
"Eternal nothingness is fine if you happen to be dressed for it."
-- Woody Allen.
 

Re:How to pass a reference to a std::vector in a function.

"Andrue Cope [TeamB]" < XXXX@XXXXX.COM >wrote in message
Quote
Nate Lockwood wrote:

>Well, my face is red! There was a typo that caused another error
>that I interpreted incorrectly. I did have it right all along.

Always use a typedef. I think in three or four years of using the STL I
once came across a situation where a typedef wasn't needed for clarity
:)

typdef std::vector<std::string>TMyStringList;

void wibble( TMyString & ListOfStrings );
You get used to the syntax of the STL. It takes
a while but then you don't normally have typos like
the above <g>
 

Re:How to pass a reference to a std::vector in a function.

"Duane Hebert" < XXXX@XXXXX.COM >writes:
Quote
>typdef std::vector<std::string>TMyStringList;
>
>void wibble( TMyString & ListOfStrings );

You get used to the syntax of the STL. It takes
a while but then you don't normally have typos like
the above <g>
You're joking I see, but I agree with always using a typedef. That
way you don't have to repeat template arguments everywhere you use the
STL. It makes it so much easier to change what is held if you use the
typedef, since iterator declarations are relative to it, no
search-and-replace necessary.
--
Chris (TeamB);
 

Re:How to pass a reference to a std::vector in a function.

"Chris Uzdavinis (TeamB)" < XXXX@XXXXX.COM >wrote in message
Quote
You're joking I see, but I agree with always using a typedef. That
way you don't have to repeat template arguments everywhere you use the
STL. It makes it so much easier to change what is held if you use the
typedef, since iterator declarations are relative to it, no
search-and-replace necessary.
Yes, I was joking. I tend to use typedef as well. Especially with
something like:
std::vector <std::pair<boost::shared_ptr<spoo>, boost::shared_ptr<doh>>
Quote
)::const_iterator ...
I've actually found a couple of good reasons to do this as well.
In some more complicated template constructs, it seems to like
typedefs. Additionally, with MSVC7.1, at least, the intellisense seems to
resolve better as well.
I just don't like type defs for simple types. I had to replace a bunch
of c code from a guy who used them poorly and it was confusing.
 

Re:How to pass a reference to a std::vector in a function.

Thanks for the typedef advice. I have the application completed and
distributed (only two days work) but I think I'll modify it if for no
other reason than I'm sure I'll be asked to modify it after it's used a
little or the input data format changes.
Nate
 

Re:How to pass a reference to a std::vector in a function.

Duane Hebert wrote:
Quote
Yes, I was joking. I tend to use typedef as well. Especially with
something like:

std::vector <std::pair<boost::shared_ptr<spoo>, boost::shared_ptr<doh>>
>)::const_iterator ...

BTW do the boost libraries work with cppbuilder 6?
Nate
 

Re:How to pass a reference to a std::vector in a function.

Nate Lockwood < XXXX@XXXXX.COM >writes:
Quote
Duane Hebert wrote:
>Yes, I was joking. I tend to use typedef as well. Especially with
>something like:
>std::vector <std::pair<boost::shared_ptr<spoo>,
>boost::shared_ptr<doh>>
>>)::const_iterator ...
>

BTW do the boost libraries work with cppbuilder 6?
Many of them do, but not all. Those that don't work tend to be the
most template-heavy, using the language in ways that Borland doesn't
yet handle well.
--
Chris (TeamB);
 

Re:How to pass a reference to a std::vector in a function.

"Nate Lockwood" < XXXX@XXXXX.COM >wrote in message
Quote
Duane Hebert wrote:
>Yes, I was joking. I tend to use typedef as well. Especially with
>something like:
>
>std::vector <std::pair<boost::shared_ptr<spoo>, boost::shared_ptr<doh>>
>>)::const_iterator ...
>

BTW do the boost libraries work with cppbuilder 6?
Some do. I was using:
date_time
format
shared_ptr
dynamic_bitset
We were able to use threads but had to mess with it a
bit (mostly to run without warning in a static build though)
Also, this was with 1.30.2. IIRC we had problems
with later versions.