Alex
Alex

Reputation: 3181

Appending Vectors

Exercise P6.07 from C++ For Everyone: Write a function vector<int> append(vector<int> a, vector<int> b) that appends b after a

Ex. a is 1 4 9 16 and b is 9 7 4 9 11 then it returns 1 4 9 16 9 7 4 9 11

My Function

vector<int> append(vector<int> a, vector<int> b)
{
    vector<int> appended;

    for (unsigned int i = 0; i < a.size(); i++)
    {
        appended.push_back(a[i]);
    }

    for (unsigned int i = 0; i < b.size(); i++)
    {
        appended.push_back(b[i]);
    }

    return appended;
}

One of my tries:

int main()
{
    cout << "Enter some numbers: ";
    int input, input2;
    vector<int> a, b;
    while (cin >> input)
    {
        if (cin.fail())
        {
            cout << "Enter some numbers: ";
            while (cin >> input2)
            {
                if (cin.fail()) {break;}
                else {b.push_back(input2);}
            }
        }
        else {a.push_back(input);}
    }
    return 0;
}

How would I use cin to get the vectors a and b when I run the main() function?

Upvotes: 0

Views: 5551

Answers (3)

Robᵩ
Robᵩ

Reputation: 168616

You ask two questions: How do I append two vectors, and how do I input two sets of numbers.

As for the first, I would use std::vector::insert as others have described. As for the second, I have two alternatives.

You could use a sentinel value (-1, say, if all of you other numbers are positive) to indicate the end of the first list. If that is not an option, you could read the first set of numbers on a single line instead of from multiple lines.

Here are two programs that do what you are trying to do.

First, a program that uses -1 as a sentinel value:

#include <iostream>
using std::cin;
using std::cout;

#include <iterator>
using std::ostream_iterator;

#include <vector>
using std::vector;


int main()
{
    int i;
    vector<int> v1;
    while(cin >> i) {
        if(i == -1)
            break;
        v1.push_back(i);
    }
    vector<int> v2;
    while(cin >> i)
        v2.push_back(i);

    v1.insert(v1.end(), v2.begin(), v2.end());

    copy(v1.begin(), v1.end(), ostream_iterator<int>(cout, ", "));
} 

Next, a program that reads the first set of numbers from a single line, and the second set of numbers from the next line:

#include <iostream>
using std::cin;
using std::cout;
using std::getline;
using std::ostream;
using std::istream;

#include <sstream>
using std::stringstream;

#include <iterator>
using std::ostream_iterator;

#include <vector>
using std::vector;

#include <string>
using std::string;


vector<int>
fetch(ostream& os, istream& is)
{       
    vector<int> result;
    os << "Enter several values, all one one line:\n";
    string line;
    getline(is, line);
    stringstream sline(line);
    int i;
    while(sline >> i)
        result.push_back(i);
    return result;
}

int main()
{
    vector<int> v1(fetch(cout, cin));
    vector<int> v2(fetch(cout, cin));

    v1.insert(v1.end(), v2.begin(), v2.end());
    copy(v1.begin(), v1.end(), ostream_iterator<int>(cout, ", "));
    cout << "\n";
}

Upvotes: 1

Sarfaraz Nawaz
Sarfaraz Nawaz

Reputation: 361302

This isn't an answer, but I would like to point out an interesting thing about this:

while (cin >> input)
{
    if (cin.fail())
    //...
}

I don't see any point of if inside the while. If cin>>input fails, the loop will break even before executing if.

That means, the while block in your code, reduced to this:

while (cin >> input)
{
    a.push_back(input);
}

This is equivalent to your while block!

Upvotes: 0

wilhelmtell
wilhelmtell

Reputation: 58667

You need to clear the error flags of std::cin if you wish to use it again after it fails.

As for appending, why not reserve and insert?

void append(std::vector<int>& a, const std::vector<int>& b)
{
    a.reserve(a.size() + b.size());
    a.insert(a.end(), b.begin(), b.end());
}

Upvotes: 3

Related Questions