0

Could we add elements in a vector<int> n; with cin >> n[i]; in a loop in C++?

Just interested in knowing and it's my first experience using stack overflow.

New contributor
volodkya is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
6
  • 7
    Maybe you should describe what you are trying to do, instead of asking whether cin >> n[i] will do it. Commented 18 hours ago
  • std::size_t size; std::cin >> size; std::vector<int> values(size); for(int& value : values) { std::cin >> value; }, Commented 17 hours ago
  • 1
    How do you know how many elements to add? How do you know when to stop adding elements? Commented 16 hours ago
  • 1
    Never give up, @Eljay . Never surrender! Commented 16 hours ago
  • 1
    This question is similar to: How can I use std::copy to read directly from a file stream to a container?. If you believe it’s different, please edit the question, make it clear how it’s different and/or how the answers on that question are not helpful for your problem. Commented 13 hours ago

2 Answers 2

4

If you don't know in advance how many items will end up in the vector you need to resize it as you read data. That's a job for a back inserter:

std::vector<int> n;
auto target = std::back_inserter(n);
while (std::cin >> *target++)
    ;

Because target is a back inserter, storing a value into *target appends the value to the vector with push_back, so each element gets properly inserted at the end of the vector. The ++ doesn't actually do anything in this case, because push_back always appends, but it's there so that the code looks like any normal use of an output iterator. If you knew the size in advance you'd skip the push_back, and you'd need the ++:

std::vector<int> n(10);
auto target = n.begin();
while (target != n.end())
    std::cin >> *target++;

Note that the two input statements are identical. That's what iterators are all about.

Sign up to request clarification or add additional context in comments.

6 Comments

Just some notes (no downvote in any way) The use of back_inserter is a good option, but with the logic shown it is not possible to stop providing input though. And for the second option the use of iterators is also not really needed if you use a range based for loop.
anything in the stream that cannot be parsed as int will make the while loop stop
And, just to underscore it, when the input comes from a file (either using std::ifstream rather than the console, or by redirecting the console input to come from a file), trying to read past the end of the file will fail and terminate the input.
When the stream extractor fails (e.g., no valid input) the loop terminates. That's how you stop providing input. The second version demonstrates the abstraction that iterators provide: extracting from the stream into the vector uses the same code in both cases.
Generally correct answer, but why do that, when istream_iterator can be used to construct the vector?
The question asks about std::cin >> n[i] in a loop. There are lots of ways to read data into a vector, but these two examples are the closest to what was asked.
2

No. You cannot insert an element into a vector like that. n[i] can only access an element that is already present in the vector. It returns a reference to the i-th element. If there is one, you can use std::cin to assign it a value:

std::vector<int> n;
n.resize(24);
std::cin >> n[4];

This can also be used in a loop:

std::vector<int> n;
n.resize(24);
for (size_t i=0; i<n.size(); ++i) {
    std::cin >> n[i];
}

However, you shouldn't write the loop like this, but use a range based for loop instead. It is less error prone:

for (auto& elem : n) {
    std::cin >> elem;
}

You can also insert elements one by one via push_back or insert. However, if possible you should prefer one resize over many insertions (at least for int, for class types the situation is different).

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.