2

In a C++ application, the arguments are all stored in a char* array, like so:

int main(int argc, char* argv[])
{
    ...
}

However, a lot of people prefer the convenience of string manipulation, but it would be a hastle to have to convert every char* into a std::string every time. So my question is, how do convert a char*[] into a std::string[], so that you don't have to convert them all individually as you progress in your program?

1
  • Read the chapter on loops. Commented Oct 3, 2016 at 3:53

2 Answers 2

6

If you accept std::vector, you can use its range constructor.

std::vector<std::string> args(argv, argv + argc);
Sign up to request clarification or add additional context in comments.

2 Comments

That would be a std::vector<std::string> args(...)
@woolstar Edited. Thanks.
1

You can loop through the arguments.

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

   std::string *s = new std::string[argc];
    for (int i = 0; i < argc; ++i)
        s[i] = argv[i];

}

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.