2

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...

0

4 Answers 4

9

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";
  }
}
Sign up to request clarification or add additional context in comments.

4 Comments

@Nawaz it's an example code to show how to modify the collection, not meant to be 100% correct code
Instead of collection.size() > 0, the !collection.empty() sounds better. ;-)
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
#include <vector> should be added for this method to work.
8

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.

1 Comment

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!)
6

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.

7 Comments

@MooingDuck ehm, what? How is it not necessary?
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.
@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.
Ah, you mean the template is needed for the function to accept arbitrary array sizes. That's true.
|
1
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.