How to Cin Values into a Vector

cin N elements to vector C++

Don't you want the user to enter the number of elements to be entered?

#include <iostream>
#include <vector>

using namespace std;

int main()
{
size_t size; int i;
vector<int> product;

vector<int> v1;
vector<int> v2;

int input, input2;

cout << "Enter vector size: ";
cin >> size;

cout << "Enter values for first vector: " << endl;
i = 0;
while (i++ != size) {
cin >> input;
v1.push_back(input);
}
cout << "Enter values for second vector: " << endl;
i = 0;
while (i++ != size) {
cin >> input2;
v2.push_back(input2);
}

int result = 0;
if (size >= 1) {
result += v1[0] * v2[0];
for (int i = 1; i < size; ++i)
result -= v1[i] * v2[i];
}

cout << result << endl;

system("pause");
return 0;
}

How to read cin into a vector

Are you sure you are hitting Ctrl-D to send the EOF properly? The following code seems to work:

int main()
{
vector<string> words;
std::string inputString;
while (cin >> inputString)
{
words.push_back(inputString);
}

vector<string>::iterator it;
for (it = words.begin(); it != words.end(); it++)
{
cout << *it << "\n";
}

return 0;
}

Is there multiple ways to cin vector elements?

vector<int> q2(2) makes a vector q2 that has 2 default-constructed int elements in it. You don't need to push_back() elements, because it makes 2 elements for you when it is constructed.

cin >> q2[b] merely edits the int elements that already exist in the vector.

taking input into vector using for-range loop

std::vector have 2 sizes.
One is actual-used size and the other is reserved-size.

Below creates a vector with actual-size n.

std::vector<int> v(size_type n)

While this creates a vector with empty size and reserved size n.

std::vector<int> v;
v.reserve(n);

std::vector<T>::push_back() increases the actual-size by 1 at the end.

So your code is

  1. Create a vector with actual-size c.
  2. Add element at the back.

So you should replace your code to

std::vector<int> vec(c);
for(auto& i : ivec){
cin>>i;
}

This creates a vector with size c and modifies its element.

Or

std::vector<int> vec;
for(int i=0; i<c; ++i) {
int temp;
std::cin >> temp;
vec.push_back(temp);
}

where this creates an empty vector and pushes the element at the end.

See cppreference for more info.

cin strings into a Vector (c++)

Use a do...while loop:

do {
if (!(cin >> keyinput)) {
// error handling...
break;
}
if (keyinput == ".") break;
words.push_back(keyinput);
}
while (true);

How can I take input from user and store it in a vector?

This is how you take inputs in a vector:

std::vector<int> numbers;
for(int i = 0; i < /*6 in your case*/; i++) {
int temp;
if(scanf("%d",&temp)) {
numbers.push_back(temp);
}
else {
std::cerr << "something wrong with vector" << "\n";
}
}

Another way would be using a for-each loop. But, you need to know the size before hand in this case:

std::vector<int> numbers(size);
for(auto& elem : numbers) {
std::cin >> elem;
}

For more information, take a look at How does c++ std::vector work?

How to read from cin into a vector

First of all, you need to resize your vector to the limits (rowSize, colSize).

vector<vector<char> > mine_field (rowSize, vector<char> (colSize));

Analogously, you can initialize all positions using this same declarations.

vector<vector<char> > mine_field (rowSize, vector<char> (colSize, 'a'));

All positions in the mine_field vector will be initialize with the char a.

To read from cin, just read normally:

for (int i = 0; i < rowSize; i++){
for (int j = 0; j < colSize; j++){
cin >> mine_field[i][j];
}
}`

Pushing back into a vector of pairs from cin giving wrong results

Use only this vector<pair<int, string>> o; Yours will end up with 2n elements in the vector or you can

int n;
cin >> n;
vector<pair<int, string>> o (n, make_pair(0, " "));

for (int a0 = 0; a0 < n; a0++)
{
int x;
string s;
cin >> x >> s;

auto& it = o.at(a0);
(it.first) = x;
it.second = s;
//o.push_back(make_pair(x, s));
}


Related Topics



Leave a reply



Submit