Inputting elements of unknown type into a vector-Collection of common programming errors

There are two aspects to this question: parsing & sorting.

  • You can use regular expressions to check the user-input data-types.
  • You can use cin to parse the data.

First: realise that you cannot necessarily know the type of your users input until you have received all of it ~eg: consider a list of user names :

728278243
390349346
495045594
elizabeth

Hence, best not to assume to know best about the incoming data (can lead to a frustrating user-experience) but instead, prefer to treat everything as potentially a string. Store all raw input as strings so you can output in same format as input. you can use say, an enumerated type to switch inside a sort comparator or consider using a mutliset/multimap. here you will be building an ordered set. so there is no need to sort. NB: the complexity for constructing an ordered set of N elements or, for a single sort on N unsorted list elements, is roughly equivalent ~> NlogN

For your task-in-hand, it hardly matters but in reality depending upon upon how the list is used, one or other approach will be far more appropriate in performance terms.

If you have already used the likes of std::vector then std::multimap shouldn’t be too scary. Loosely, it is an associated array of key-value pairs. the multi here meaning it can store multiple elements with the same key (which here, you want).

In this example i am using the boost regex library in order to determine some funky input data-types.
(eg: sudo apt-get install libboost-regex1.46-dev)

This regex might seem arcane but there are many examples on the i/web for practically every conceivable pattern. [NB: C++11 regex is pretty-much a drop-in replacement for boost regex. ie: boost regex should be forward-compatible with the emerging C++11 standard]

blah.cpp:

#include 
#include 
#include 
#include 
#include 
#include 
#include     
//NB: GNU gcc added *experimental support for regular expressions in TR1 v 4.3.0.
//    compile with:  -std=c++0x

using namespace std;
using namespace boost;

//some example input data-types (perhaps notably missing a date!) 
const regex re_char("[^0-9]", regex_constants::extended); //non numeric chars
const regex re_digit("[[:digit:]]+", regex_constants::extended); //a string of only digits in range [0..9] ~ie: Z+
const regex re_xdigit("0[xX][[:xdigit:]]+", regex_constants::extended); //support hex iff starts with '0x' or '0X'
const regex re_float("[-+]?[0-9]*\\.?[0-9]+([eE][-+]?[0-9]+)?", regex_constants::extended); //all kinds of numbers


int main(int argc, char** argv)
{    
    int i, countc=0;
    double d;
    string str;
    int element_count;    

    do
    {
        cout > element_count) break;
        cin.clear();
        cin >> str;
        cout  d;    
            list_fp.insert(pair(d, str));        
            list_num.insert(pair(d, str)); 
        } 

        if (regex_match(str, re_char)) countc++;      
        list_str.push_back(str);
    }    

    cout

Originally posted 2013-11-09 22:07:04.