Passing a array of std::string by reference

20,254

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.

Share:
20,254
Momergil
Author by

Momergil

Updated on July 25, 2020

Comments

  • Momergil
    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
    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
    Nawaz almost 11 years
    Instead of collection.size() > 0, the !collection.empty() sounds better. ;-)
  • wlyles
    wlyles almost 11 years
    As 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 sure collection.size() > 100, etc, etc
  • Mooing Duck
    Mooing Duck almost 11 years
  • juanchopanza
    juanchopanza almost 11 years
    @MooingDuck ehm, what? How is it not necessary?
  • Mooing Duck
    Mooing Duck almost 11 years
    it's not necessary because a reference to std::string[10] doesn't decay to a pointer. See my code where it compiles and disallows a std::string[3] to be passed in.
  • juanchopanza
    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
    Mooing Duck almost 11 years
    Ah, you mean the template is needed for the function to accept arbitrary array sizes. That's true.
  • juanchopanza
    juanchopanza almost 11 years
    @MooingDuck yes, but you made me realise I hadn't written that. I have tried to clarify it.
  • Sean Cline
    Sean Cline almost 11 years
    For 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
    Momergil almost 11 years
    Actually 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
    Kirk Backus almost 11 years
    I'm glad I could help!
  • Admia
    Admia over 7 years
    #include <vector> should be added for this method to work.