Passing a array of std::string by reference
Solution 1
Here's how!
//Change the 10 to whatever size you'd like
void changeStr(std::string (&str)[10]) {
}
This of course, is for a static size, the other answers, however, are better methods accomplishing what you need with flexibility.
Solution 2
Since you're using C++ you probably want to pass a collection of values by reference instead of a collection of references by reference. The easiest way to achieve this is to use std::vector<T>
void changeStr(std::vector<std::string>& collection) {
if (collection.size() > 0) {
collection[0] = "hello world";
}
}
Solution 3
One approach would be to pass a reference to an std::array<std::string, N>
where N
is the size of the array. You can use a function template to deduce N
:
#include <array>
template <size_t N>
void changeStr(std::array<std::string, N>& strings)
{
// access strings[i] for i >= 0 and i < N
}
alternatively, you can pass fixed size plain arrays, again using function template:
template<size_t N >
void changeStr( std::string (&strings)[N] )
{
// access strings[i] for i >= 0 and i < N
}
Note that the template is necessary here to allow for the function to work with fixed sized arrays of different sizes. The template allows you to keep the size information without having to worry about it's actual value.
Solution 4
void changeStr(std::string pStrings[], int num)
You can pass any C array of any size. If the changeStr function needs to know the size, you need to pass it as a size parameter. Note that personally I prefer to use a vector.
Momergil
Updated on July 25, 2020Comments
-
Momergil almost 4 years
I'ld like to create a function that passes not a std::string by reference to be modified,
void changeStr(std::string &str) { str = "Hello World!"; }
, but rather an entire, fixed-sized array of std::strings (the function will do exactly the same: attribute some specific strings to each space in the array). But I don't know which is the appropriate syntax...
-
JaredPar almost 11 years@Nawaz it's an example code to show how to modify the collection, not meant to be 100% correct code
-
Nawaz almost 11 yearsInstead of
collection.size() > 0
, the!collection.empty()
sounds better. ;-) -
wlyles almost 11 yearsAs an extension of what @Nawaz said: If you're going to index directly like this, make sure that the vector is at least big enough for your biggest index. So if you change
collection[100]
, make surecollection.size() > 100
, etc, etc -
Mooing Duck almost 11 years
-
juanchopanza almost 11 years@MooingDuck ehm, what? How is it not necessary?
-
Mooing Duck almost 11 yearsit's not necessary because a reference to
std::string[10]
doesn't decay to a pointer. See my code where it compiles and disallows astd::string[3]
to be passed in. -
juanchopanza almost 11 years@MooingDuck the point is that you can use the same function template for sizes other than
10
. I realise my wording is poor, but the template is needed. -
Mooing Duck almost 11 yearsAh, you mean the template is needed for the function to accept arbitrary array sizes. That's true.
-
juanchopanza almost 11 years@MooingDuck yes, but you made me realise I hadn't written that. I have tried to clarify it.
-
Sean Cline almost 11 yearsFor the sake of mentioning it, C++14 will likely have
std::dynarray
which has a fixed size at construction rather than being baked into the type. -
Momergil almost 11 yearsActually that's what I needed. I agree that with flexibility in mind, the other methods are better, but I was thinking about a pre-fixed sized array, so your answer end up being the simplest and the best. Thanks! (And thanks for the others as well!)
-
Kirk Backus almost 11 yearsI'm glad I could help!
-
Admia over 7 years
#include <vector>
should be added for this method to work.