Eiiki
Eiiki

Reputation: 320

Make array as big as the input using 'cin' in c++

I'm prettty new to c++ programming and haven't got grip on the most basic techniques.

My problem is that I want to read characters into a array and make that array just as long as the input. For example if the input would be 'a', 'b' and 'c' then the length of the array would be 3. In java this would be like this for integers using StdIn package:

int[] n = StdIn.readInts(); //enter the numbers 5,6,7,8 f.x.
StdOut.println(n.length); //would print 4

Hopefully I was clear enough.

Upvotes: 1

Views: 1698

Answers (4)

Rok Kralj
Rok Kralj

Reputation: 48735

If you want to read all the numbers the user inputs, then you can do this:

int number;
std::vector<int> numbers;

while (std::cin >> number)
   numbers.push_back(number);

Upvotes: 0

ShinTakezou
ShinTakezou

Reputation: 9671

If you take a look at the implementation of StdIn.readInts, you see it can be described in two steps:

  • read all the "tokens" from the standard input, where "tokens" are strings (for every line) split by a regex, then
  • for each tokens, interpret it as integer (parseInt) and "append" to an array (since it knows the number of tokens from the previous step, it creates an array that has enough room for the right number of ints)

This can be mimicked in C++ coding a similar class, with similar methods, or doing the above steps in the moment you need them, considering that std::vector are maybe closer to Java arrays than C++ arrays are, and that std::vector can grow dinamycally.

In your example, to read a list of ints separated by "white spaces",

#include <iostream>
#include <vector>

int main()
{
    std::vector<int> n;
    int tmp;

    while(std::cin >> tmp) {
        n.push_back(tmp);
    }

    std::cout << n.size() << "\n";

    return 0;
}

suffices (but look at other answers, there's a nice std::copy solution in the air), but if you want to imitate Java behaviour (if I intended it correctly reading swiftly the code), something like

#include <iostream>
#include <vector>
#include <sstream>

int main()
{
    std::vector<int> n;

    // read in the "pieces" as string
    std::vector<std::string> ns;    
    std::string tmps;
    while(std::cin >> tmps) {
        ns.push_back(tmps);
    }

    // pre C++11 way... try to interpret the "pieces" as int
    for (std::vector<std::string>::const_iterator it = ns.begin();
         it != ns.end();
         ++it) 
    {
        int tmp;
        if (std::istringstream(*it) >> tmp) {
            n.push_back(tmp);
        }
    }

    std::cout << n.size() << "\n";

    // post C++11 way, just to test...
    for (auto v : n) std::cout << v << "\n";

    return 0;
}

should be closer to what Java StdIn.readInt() does (again, according to my understanding of it reading the code on the fly).

Upvotes: 0

Shoe
Shoe

Reputation: 76240

You can easily use std::vector to store your input. Using push_back or emplace_back, for example, you'll be able to push as many elements as you need. You'll be able to retrieve the size of the array via the size member function.

Here's an example:

std::vector<char> vector;
for (int i = 0; i < 3; i++) {
    char buffer;
    std::cin >> buffer;
    vector.push_back(buffer);
}

The above code will ask for 3 characters and store them into vector. The vector interface has a convenient "array-like" interface that you can use. For example to retrieve the third element you can use vector[2].

Upvotes: 2

Some programmer dude
Some programmer dude

Reputation: 409176

In C++ it's very easy to do:

std::vector<int> int_vector;
std::copy(std::istream_iterator<int>(std::cin),
          std::istream_iterator<int>(),
          std::back_inserter(int_vector));

References:

Upvotes: 2

Related Questions