Note that there are some explanatory texts on larger screens.

plurals
  1. POunordered_map adding returns to strings read in from a file c++
    text
    copied!<p>I have a text file, formatted:</p> <pre><code>"stringx, stringy" "stringx, stringy" </code></pre> <p>e.t.c.</p> <p>I have put my code below to show how I'm reading into my unordered map, parsing the text and making stringx the key with stringy the value.</p> <p>For the problem I am trying to solve I need to also have another map, in the reverse order i.e key being stringy, and value being stringx. </p> <p>The values are getting entered correctly, when i output straightData, you see </p> <pre><code>"stringx:stringy" </code></pre> <p>but when i output reverseData, it's instead</p> <pre><code>"stringy :stringx" </code></pre> <p>I don't know if this is an issue for the overall scope of my program (i need to get one key, append it to a list, and then take the value from the key and use that to lookup another key/value pair). I'm just thinking however, if it automatically has a newline in the data, would </p> <pre><code>stringx </code></pre> <p>be the same as</p> <pre><code>(space) stringx </code></pre> <p>?</p> <p>I don't know if i'm even making sense, but as always all help would be massively appreciated!</p> <pre><code>typedef unordered_map&lt;string, string&gt; mymap; int main() { mymap map1; mymap map2; list &lt;pair&lt;string, string&gt;&gt; list1; ifstream myFile; myFile.open("values.txt"); string forParse; string first, second; while (getline(myFile, forParse)) { stringstream x(forParse); getline(x, first, ','); getline(x, second); map1[first] = second; map2[second] = first; } for (auto&amp; x: map2) cout &lt;&lt; x.first &lt;&lt; ":" &lt;&lt; x.second &lt;&lt; endl; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload