-3

How to pass multiple string parameter in C++ without using vector and pass it like an argument?

I want to do something like this.

void MyClass::go()
{
  check("string1","string2","string3");
}

//guessing
void check(string values,...)
{
 //how I can output each value here?
}

EDITED: Thanks for the answers, but my goal is to pass string paramaters with undefine sizes. it means I don't need to input some int for it size. I don't want to use vectors :)

Garlen
  • 426
  • 1
  • 5
  • 17
  • 2
    Just give `check` that many parameters. – chris Jun 23 '14 at 04:38
  • 1
    possible duplicate of [Variable number of arguments in C++?](http://stackoverflow.com/questions/1657883/variable-number-of-arguments-in-c) – Matt Coubrough Jun 23 '14 at 04:40
  • 1
    Do you want to call `check` with exactly three parameters all the time? Or do you need to call with different number of arguments from different places? – R Sahu Jun 23 '14 at 04:47

4 Answers4

1

In my opinion the best solution would be to actually use a vector. This way you have type safety, and you can pass in any number of strings.

You can also use variadic templates.

If you exactly know how many strings you want to pass in, you can have a parameter list of that many strings. This is pretty tedious though, if you change your mind you need to rewrite your function (and every code using it).

The problem with var args is that it's not typesafe, the compiler can't verify, that you want to pass in a bunch of strings to your function.

Zsolt
  • 582
  • 2
  • 5
0
void check (const string &string1, const string &string2, const string &string3)
{
    cout << string1 << " " << string2 << " " << string3 << endl ;
}
user3344003
  • 20,574
  • 3
  • 26
  • 62
  • Thanks for the answers but my goal is to pass multiple string with indefinite size, it's just an example. – Garlen Jun 24 '14 at 11:29
0

In c & c++, this can be achieved by something called variable arguments. Note that for varargs, you do need to provide atleast one argument which is the number of strings, at the time of invoking the function. A variable argument facilitates the passing of multiple arguments, without actually knowing the number of arguments to pass.

For this, you can include stdarg.h. This gives you the data type: va_list, and associated functions:

#include <cstdarg>
#include <iostream>

using namespace std;

void check ( int num, ... )
{
    va_list arguments;  //a place to store the arguments values

    va_start ( arguments, num );           // Initializing arguments to store all values after num

    for ( int x = 0; x < num; x++ ) {     // Loop until all numbers are added
      cout << va_arg ( arguments, string ); // Prints the next value in argument list to sum.
    } 

    va_end ( arguments );                  // Cleans up the list
}

void MyClass::go()
{
  check(3,"string1","string2","string3");
}

num is the number of arguments, that you'll provide.

va_start populates the va_list with the arguments (in this case the strings) that you provide.

va_arg() is used as to access those arguments in the list.

Finally, va_end() is used for cleaning up the va_list.

aceBox
  • 711
  • 2
  • 13
  • 30
  • 4
    C++ does this much better with initializer lists and variadic templates. There's absolutely no point in using this type-unsafe method. – chris Jun 23 '14 at 04:54
  • In c++, you can infact also use operator overloading, or STL for that purpose. What I have mentioned will be good for C. – aceBox Jun 23 '14 at 04:56
  • 2
    You cannot pass non-POD types using `stdarg.h` style varargs. `string` is not a POD (assuming we're talking about `sdt::string`, so it's undefined behavior to pass one as a vararg. – Michael Burr Jun 23 '14 at 07:26
  • In this example, the string parameter values would be passed as `char*`, so you would have to use `va_arg(char*)` instead of `va_arg(string)`. – Remy Lebeau Jun 23 '14 at 22:22
  • Thanks for the answer, is it possible to this without inputting any size and just strings? – Garlen Jun 24 '14 at 11:30
  • The size is the "last" named argument, that va_start needs to locate where the arguments are. You have to pass that to tell va_start to initialize the list properly – aceBox Jun 24 '14 at 12:42
0

Either you may use va_list or variadic templates in C++11. It depends on your context.

Doonyx
  • 580
  • 3
  • 12