I need an iterator to a specific character in a std::string, without looping from the beginning.
Is this the best way to do it?
std::string s("abcdefg...");
size_t id = 5;
std::string::const_iterator it = s.begin();
std::advance(it, id - 1);
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You can also simply do:
although it should have more or less the same performance of
std::advance, since, ifitis a random access iterator,advanceusesoperator+(otherwise it resorts to looping; this happens e.g. forlistiterators).