{"id":738,"date":"2022-08-30T15:06:21","date_gmt":"2022-08-30T15:06:21","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/09\/inputting-elements-of-unknown-type-into-a-vector-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:06:21","modified_gmt":"2022-08-30T15:06:21","slug":"inputting-elements-of-unknown-type-into-a-vector-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/inputting-elements-of-unknown-type-into-a-vector-collection-of-common-programming-errors\/","title":{"rendered":"Inputting elements of unknown type into a vector-Collection of common programming errors"},"content":{"rendered":"<p>There are two aspects to this question: parsing &amp; sorting.<\/p>\n<ul>\n<li>You can use <em>regular expressions<\/em> to check the user-input data-types.<\/li>\n<li>You can use <code>cin<\/code> to parse the data.<\/li>\n<\/ul>\n<p><strong>First:<\/strong> realise that you cannot <em>necessarily<\/em> know the <em>type<\/em> of your users input until you have received all of it ~eg: consider a list of user names :<\/p>\n<pre><code>728278243\n390349346\n495045594\nelizabeth\n<\/code><\/pre>\n<p>Hence, best not to assume to know best about the incoming data (<em>can lead to a frustrating user-experience<\/em>) 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 <em>or<\/em> consider using a <code>mutliset\/multimap<\/code>. here you will be building an <em>ordered<\/em> set. so there is no need to sort. NB: the complexity for constructing an ordered set of N elements <em>or<\/em>, for a single sort on N unsorted list elements, is roughly equivalent ~&gt; NlogN<\/p>\n<p>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.<\/p>\n<p>If you have already used the likes of <code>std::vector<\/code> then <code>std::multimap<\/code> shouldn&#8217;t be too scary. Loosely, it is an associated array of key-value pairs. the <em>multi<\/em> here meaning it can store multiple elements with the <em>same<\/em> key (which here, you want).<\/p>\n<p>\nIn this example i am using the <em>boost<\/em> regex library in order to determine some <em>funky<\/em> input data-types.<br \/>\n(eg: <code>sudo apt-get install libboost-regex1.46-dev<\/code>)<\/p>\n<p>This <em>regex<\/em> might seem arcane but there are many examples on the i\/web for practically every conceivable pattern. [<em>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<\/em>]<\/p>\n<p>\n<strong>blah.cpp:<\/strong><\/p>\n<pre><code>#include \n#include \n#include \n#include \n#include \n#include \n#include     \n\/\/NB: GNU gcc added *experimental support for regular expressions in TR1 v 4.3.0.\n\/\/    compile with:  -std=c++0x\n\nusing namespace std;\nusing namespace boost;\n\n\/\/some example input data-types (perhaps notably missing a date!) \nconst regex re_char(\"[^0-9]\", regex_constants::extended); \/\/non numeric chars\nconst regex re_digit(\"[[:digit:]]+\", regex_constants::extended); \/\/a string of only digits in range [0..9] ~ie: Z+\nconst regex re_xdigit(\"0[xX][[:xdigit:]]+\", regex_constants::extended); \/\/support hex iff starts with '0x' or '0X'\nconst regex re_float(\"[-+]?[0-9]*\\\\.?[0-9]+([eE][-+]?[0-9]+)?\", regex_constants::extended); \/\/all kinds of numbers\n\n\nint main(int argc, char** argv)\n{    \n    int i, countc=0;\n    double d;\n    string str;\n    int element_count;    \n\n    do\n    {\n        cout &gt; element_count) break;\n        cin.clear();\n        cin &gt;&gt; str;\n        cout  d;    \n            list_fp.insert(pair(d, str));        \n            list_num.insert(pair(d, str)); \n        } \n\n        if (regex_match(str, re_char)) countc++;      \n        list_str.push_back(str);\n    }    \n\n    cout<\/code><\/pre>\n<p id=\"rop\"><small>Originally posted 2013-11-09 22:07:04. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>There are two aspects to this question: parsing &amp; 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 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-738","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/738","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=738"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/738\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=738"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=738"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=738"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}