Reputation: 1669
Take string input from user and than put it in a vector(first) in c++ for example if string is"038493" and when I access first[0] it will give me 0. How can we do it faster.
string p;
cin>>p;
for(i=0;i<n;i++){
first.push_back(p[i]);
}
I used this but this is slow, tell me good method to do it.
Upvotes: 0
Views: 73
Reputation: 304122
Fastest: do nothing. std::string
can basically function as std::vector<char>
for most purposes, and doing literally nothing is always going to be faster than doing something.
std::string p;
cin >> p;
// just use p
// p[0] is the first char, e.g. '0', so you don't have to do anything
// p[1] is the second, etc.
// p.size() is the size
Next fastest: just directly construct your vector:
std::vector<char> data(p.begin(), p.end());
That will make sure to do only one allocation of the correct amount (of at least p.size()
), and will copy the string into the vector as efficiently as feasible.
Next: add each character one at a time:
// C++11
std::vector<char> data;
for (char c : p) {
data.push_back(c);
}
// C++03
for (size_t i = 0; i < data.size(); ++i) {
data.push_back(p[i]);
}
The longer the string, the more extra allocations/copies will be done as opposed to the direct-construction method. The exact amount is implementation dependent, but this approach will always be slower - even your particular implementation default-constructs a vector with large initial capacity (since you would at least have to branch on each push_back
to check if you had to resize
... whereas the direct construction wouldn't even have to do that).
Upvotes: 0
Reputation:
//`for(char& c : p) { // first.push_back(c); //}
Apologies, I was mistaken. I think if you use this constructor for the vector it would be the fastest way. Juan has the right idea.
std::vector<char> first(p.begin(), p.end());
Upvotes: 0
Reputation: 497
first.insert(first.end(), p.begin(), p.begin() + n);
if n is actually the length of the whole string, it'd be better to use:
first.insert(first.end(), p.begin(), p.end());
Upvotes: 1