So now I have a
int main (int argc, char *argv[]){}
how to make it string based? will int main (int argc, std::string *argv[])
be enough?
You can't change main's signature, so this is your best bet:
#include <string>
#include <vector>
int main(int argc, char* argv[])
{
std::vector<std::string> params(argv, argv+argc);
// ...
return 0;
}
If you want to create a string out of the input parameters passed, you can also add character pointers to create a string yourself
#include <iostream>
#include <string>
using namespace std;
int main(int argc, char* argv[])
{
string passedValue;
for(int i = 1; i < argc; i++)
passedValue += argv[i];
// ...
return 0;
}
You can't do it that way, as the main function is declared explicitly as it is as an entry point. Note that the CRT knows nothing about STL so would barf anyway. Try:
#include <string>
#include <vector>
int main(int argc, char* argv[])
{
std::vector<std::string> args;
for(int i(0); i < argc; ++i)
args.push_back(argv[i]);
// ...
return(0);
}; // eo main
That would be non-standard because the Standard in 3.6.1
says
An implementation shall not predefine the main function. This function shall not be overloaded. It shall have a return type of type int, but otherwise its type is implementation-defined. All implementations shall allow both of the following definitions of main:
int main() { /* ... */ }
and
int main(int argc, char* argv[]) { /* ... */ }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With