0

I was trying to read in a list of numbers and letters into a std::vector<char>. As it produced errors, I tried only to read in numbers:

#include <vector>
#include <iostream>
using namespace std;
int main(int argc, char const *argv[])
{
    vector<char> zahlen;
    for (int i = 0; i < 10; ++i)
    {
        zahlen.push_back(i);
    }
    for (int i = 0; i < zahlen.size(); ++i)
    {
        cout<<zahlen[i];
    }
    cout<<endl;
    return 0;
}

This produces some strange output. What am I doing wrong? How can I store multiple types of data in a vector or another container?

EDIT:

cout<<(int)zahlen[i];

This did the job for displaying the content, but how do I identify the different types of data?

Magnus
  • 1,550
  • 4
  • 14
  • 33
  • 1
    "How can I store multiple types of data in a vector or another container?" You can't directly (in a `std::vector`). What do you want to achieve (why do you want to store numbers *and* letters in the same `vector`)? – dyp Oct 11 '13 at 22:02
  • 2
    "This produces some strange output." as the elements of the `vector` are of type `char`, using `cout << zahlen[i]` outputs the numbers interpreted as characters. – dyp Oct 11 '13 at 22:03
  • 1
    What you want to do is `cout << (int)zahlen[i];`. (Or `cout << static_cast(zahlen[i]);`, whatever floats your boat). Better yet, using `std::vector` should solve the problem. :V – user123 Oct 11 '13 at 22:04
  • So (int)zahlen[i] did it well, but how do I identify the type of the input? – Magnus Oct 11 '13 at 22:10
  • What's "strange output"? – Kerrek SB Oct 11 '13 at 22:13
  • @KerrekSB nothing a terminal can print out. There's just a blank line and an error sound. – Magnus Oct 11 '13 at 22:19
  • `std::vector` is a homogeneous container. It can only hold elements of the same type. What you're looking for is a heterogeneous container. The standard C++ library doesn't offer such a container, so you'll have to use a 3rd party library for this, like Boost.Any (http://www.boost.org/doc/libs/1_54_0/doc/html/any.html) – Nikos C. Oct 11 '13 at 22:35
  • Using a `std::vector` as a buffer... it must be for some bad programming contest. – Havenard Oct 12 '13 at 00:26
  • Can you clarify if what you meant by "numbers"? Did you actually only mean the decimal digits, or did you mean arbitrary integer values? – jxh Oct 12 '13 at 01:58
  • @Magnus: Why is that "strange"? That's exactly what I'd expect my terminal to do with those control characters, so I'd find that "reassuringly expected". – Kerrek SB Oct 12 '13 at 09:57

1 Answers1

2

Based on your for loop, I will assume when you said "numbers", you actually meant "decimal digits".

You can use std::vector<char> to store decimal digits and letters, but they are all going to be represented as char. You can account for that by using the character encoding of your system to store the decimal digits. One simple way:

    vector<char> zahlen;
    for (int i = 0; i < 10; ++i)
    {
        zahlen.push_back('0' + i);
    }

If instead of "decimal digits" you really mean arbitrary numbers, then you need something more complicated than a char for your vector. As suggested in the comments, Boost.Any is a possibility.

jxh
  • 69,070
  • 8
  • 110
  • 193
  • 1
    `Boost.Any` is comprehensive, but it is not that difficult to put together something simpler on your own. See [this demo](http://ideone.com/IonCML) for an example. – jxh Oct 12 '13 at 00:31
  • Or just use a tuple... – JimR Oct 12 '13 at 01:24
  • @JimR: While a `tuple<>` can hold heterogeneous data, it is a static type. – jxh Oct 12 '13 at 01:40