string find in C++ String find is used to find the first occurrence of sub-string in the specified string being called upon. It returns the index of the first occurrence of the substring in the string from given starting position. The default value of starting position is 0.
Because the declaration of class string is in the namespace std. Thus you either need to always access it via std::string (then you don't need to have using) or do it as you did.
Why isn't it in the standard? Simple:
Because nobody proposed it.
Things go into the C++ standard because someone writes a proposal for it, then gets other people to vote on it and incorporate it. Unless someone actually wants it to happen, it doesn't get to happen.
And considering how trivial this feature really is:
int main(int argc, char **argv)
{
std::vector<std::string> args(argv, argv + argc);
...
}
There just isn't any real need for it. It's a convenience function, and it doesn't even make things that convenient compared to the alternative.
why in C++ the main() function is not overloaded to take an std::vector argv instead of the old char* argv[]
Because that demands dependency on <string>
library. C++'s philosophy is always "don't pay for what you don't use". If someone doesn't want the automatic memory management offered by string
then they can't be enforced.
2nd view: If some platform doesn't have any library support then you cannot start your C++ program!!
On the contrary, int
and char**
arguments are built-in and independent types. One can always write custom wrapper on main()
which does exactly whatever is the need.
Edit: On AProgrammer's comment:
Suppose that main(vector<string>)
is allowed; then if a platform conforms to all C++ features, but don't have standard library support then it will become non-standard compliant.
The main reason, I suspect, is because the usual implementation is to
make it an extern "C"
function, with varargs. In a lot of
implementations, it is the C runtime library which calls main.
There are many ways around this, but none of them seem worth the bother, given how easy it is to convert the arguments to whatever you want.
It would be trivial for a compiler to compile a main defined as
int main(std::vector<std::string> args) {
...
}
as it has been written as
int main(int __argc, char **__argv) {
std::vector<std::string> args(__argv, __argv+__argc);
...
}
It would be even about as easy to allow any container, even custom one, and not just std::vector<std::string>
.
But it isn't standard because — standard answer to why isn't this in the standard — nobody proposed it and convinced enough people that it was a good idea. I don't think there was a proposition, so there is probably no rationale for its rejection. The change is probably to simple for most of involved people to bother.
Basically, a std::vector is NOT the same (in memory layout or anything) as an array of char *. To permit this, you'd have to make the compiler recognise the new main declaration and add a wrapper on entry that created a vector of strings.
Given you were doing that, you might as well throw argc away as well, and have the declaration of main be
int main(std::vector<std::string> argv)
But it's work for the compiler. You'd have to get a lot of people who thought it was worthwhile.
Otherwise you can just do it yourself
int main(int argc, char **argv)
{
std::vector<std::string> args;
args.reserve(argc);
for (std::size_t arg = 0; arg < argc; ++arg)
{
args.push_back(argv[i]);
}
return mymain(args);
}
Code is not guaranteed to compile or work cos I just wrote it off the top of my head.
or (better, thanks to AProgrammer)
int main(int argc, char **argv)
{
return mymain(std::vector<std::string>(argv, argv + argc));
}
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