OK I was wondering what is the easiest way to do this?

Let's say I have some arguments


int main(int argc, char* argv[])

and argv[1] equals a c-style string. I want to change this to a std::string

because I want to do stuff with it and I prefer working with std::strings.

What is the easiest way to do this?

How about using std::valarray.

#include <iostream>
#include <valarray>

int main(int argc, char **argv) {
   std::valarray<std::string> arguments(argc);
   for (int i = 0; i < argc; ++i)
      arguments[i] = argv[i];

   return 0;
}
Comments
i won't use valarrays but your code helped

>How about using std::valarray.
Why? That seems like a case of using valarray for the sake of using valarray. Kind of silly, in my opinion.

Well personally I think its better suited than std::vector, and I think its too much trouble to dynamically allocate and delete an array of strings, so I think its a nice solution :]

>Well personally I think its better suited than std::vector
Ignoring that valarray is designed for numeric types, and ignoring that you can't use the majority of operations with valarray on std::string, and ignoring that you've lost nearly all benefits of the standard library by using a bastard child of the standardization process, and ignoring that the standard definition is broken and many implementations of valarray are substandard because nobody cares enough to fix it in light of exression templates, it probably is better suited. But only for your specific example, and only after ignoring numerous rather large turds that you'll end up eating later.

There are certainly cases where valarray is a superior choice, but that's only because the interface is perfectly suited to the problem and a different solution would result in more complex code. However, this is not one of those cases.

Comments
Don't you ever get tired of being right :)??

>Well personally I think its better suited than std::vector
Ignoring that valarray is designed for numeric types, and ignoring that you can't use the majority of operations with valarray on std::string, and ignoring that you've lost nearly all benefits of the standard library by using a ******* child of the standardization process, and ignoring that the standard definition is broken and many implementations of valarray are substandard because nobody cares enough to fix it in light of exression templates, it probably is better suited. But only for your specific example, and only after ignoring numerous rather large turds that you'll end up eating later.

Fair enough :icon_cheesygrin:

Well this seems to work:

#include <iostream>
#include <vector>

using namespace std;

int main ( int argc, char argv[] )
{
  vector <string> arguments ( argc );

  for ( int i = 0; i < argc; ++i )
  {
    arguments[i] = argv[i];
  } 
  return 0;
}

I tell a lie

#include <iostream>
#include <vector>

using namespace std;

int main ( int argc, char** argv )
{
  vector <string> arguments ( argc );

  for ( int i = 0; i < argc; ++i )
  {
    arguments[i] = argv[i];
    cout << arguments[i]<<endl;
  } 
  return 0;
}

It should be the above.

a little less verbose, and a little more efficient:

#include <vector>
#include <string>

int main ( int argc, char** argv )
{
  std::vector< std::string > arguments( argv, argv+argc ) ;
}
This question has already been answered. Start a new discussion instead.