How to create an std::map and save it by using the console?












-1















I want to be able to create and store values by using the console and getting user input to create those values. So I would like to be able to type in the console something like



1234

123

1


and it would save it into my map with a tuple such as



std::map<int, std::tuple<int, int>> info; 

info[1234] = { 123, 1 };


I am completely new to this and have been looking up stuff for a couple hours but I do not understand how to use << >> everywhere I look says to use those. I would like to be able to close the program and open it and the values would still be stored as well.



Any and all information would be appreciated. Thanks.










share|improve this question




















  • 2





    You should follow either an introductory book (stackoverflow.com/questions/388242/…) or some other text/course. You will not learn the language properly by looking up random stuff and gluing it together.

    – user10605163
    Nov 17 '18 at 20:16













  • Look up operator overloading and read up on it.

    – TrebuchetMS
    Nov 17 '18 at 20:42













  • Thank you both for the information I did not know there was a guide on here for learning C++.

    – creation8383
    Nov 20 '18 at 7:15
















-1















I want to be able to create and store values by using the console and getting user input to create those values. So I would like to be able to type in the console something like



1234

123

1


and it would save it into my map with a tuple such as



std::map<int, std::tuple<int, int>> info; 

info[1234] = { 123, 1 };


I am completely new to this and have been looking up stuff for a couple hours but I do not understand how to use << >> everywhere I look says to use those. I would like to be able to close the program and open it and the values would still be stored as well.



Any and all information would be appreciated. Thanks.










share|improve this question




















  • 2





    You should follow either an introductory book (stackoverflow.com/questions/388242/…) or some other text/course. You will not learn the language properly by looking up random stuff and gluing it together.

    – user10605163
    Nov 17 '18 at 20:16













  • Look up operator overloading and read up on it.

    – TrebuchetMS
    Nov 17 '18 at 20:42













  • Thank you both for the information I did not know there was a guide on here for learning C++.

    – creation8383
    Nov 20 '18 at 7:15














-1












-1








-1








I want to be able to create and store values by using the console and getting user input to create those values. So I would like to be able to type in the console something like



1234

123

1


and it would save it into my map with a tuple such as



std::map<int, std::tuple<int, int>> info; 

info[1234] = { 123, 1 };


I am completely new to this and have been looking up stuff for a couple hours but I do not understand how to use << >> everywhere I look says to use those. I would like to be able to close the program and open it and the values would still be stored as well.



Any and all information would be appreciated. Thanks.










share|improve this question
















I want to be able to create and store values by using the console and getting user input to create those values. So I would like to be able to type in the console something like



1234

123

1


and it would save it into my map with a tuple such as



std::map<int, std::tuple<int, int>> info; 

info[1234] = { 123, 1 };


I am completely new to this and have been looking up stuff for a couple hours but I do not understand how to use << >> everywhere I look says to use those. I would like to be able to close the program and open it and the values would still be stored as well.



Any and all information would be appreciated. Thanks.







c++ dictionary tuples stdmap






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 18 '18 at 0:02









TrebuchetMS

2,5431923




2,5431923










asked Nov 17 '18 at 19:53









creation8383creation8383

6




6








  • 2





    You should follow either an introductory book (stackoverflow.com/questions/388242/…) or some other text/course. You will not learn the language properly by looking up random stuff and gluing it together.

    – user10605163
    Nov 17 '18 at 20:16













  • Look up operator overloading and read up on it.

    – TrebuchetMS
    Nov 17 '18 at 20:42













  • Thank you both for the information I did not know there was a guide on here for learning C++.

    – creation8383
    Nov 20 '18 at 7:15














  • 2





    You should follow either an introductory book (stackoverflow.com/questions/388242/…) or some other text/course. You will not learn the language properly by looking up random stuff and gluing it together.

    – user10605163
    Nov 17 '18 at 20:16













  • Look up operator overloading and read up on it.

    – TrebuchetMS
    Nov 17 '18 at 20:42













  • Thank you both for the information I did not know there was a guide on here for learning C++.

    – creation8383
    Nov 20 '18 at 7:15








2




2





You should follow either an introductory book (stackoverflow.com/questions/388242/…) or some other text/course. You will not learn the language properly by looking up random stuff and gluing it together.

– user10605163
Nov 17 '18 at 20:16







You should follow either an introductory book (stackoverflow.com/questions/388242/…) or some other text/course. You will not learn the language properly by looking up random stuff and gluing it together.

– user10605163
Nov 17 '18 at 20:16















Look up operator overloading and read up on it.

– TrebuchetMS
Nov 17 '18 at 20:42







Look up operator overloading and read up on it.

– TrebuchetMS
Nov 17 '18 at 20:42















Thank you both for the information I did not know there was a guide on here for learning C++.

– creation8383
Nov 20 '18 at 7:15





Thank you both for the information I did not know there was a guide on here for learning C++.

– creation8383
Nov 20 '18 at 7:15












1 Answer
1






active

oldest

votes


















0














I've put together some sample code to approximate your task. It reads some integers from standard input, and throws it back to standard output. For your purposes, you may wish to format the output differently, and read/save it from/to a file at some point. There is probably more terse, more robust, more efficient, and prettier ways to do it, but I put this together quickly to get you started. If you find things that are strange to you, I would be happy to answer some questions, but the idea of this site is that you prove that you put some work into finding out the answers first (for example by visiting other questions, reading some books, or consulting the C++ reference at: https://en.cppreference.com/w/cpp)



#include <iostream>
#include <map>
#include <stdexcept>

using namespace std;
using MyMap = map<int, pair<int, int>>;

MyMap::value_type read_entry() {
int key, v_1, v_2;
bool started_read{false};
if ((cin >> key) && (started_read = true) && (cin >> v_1) && (cin >> v_2)) {
return {key, {v_1, v_2}};
} else if (started_read) {
throw invalid_argument("improper input");
} else {
return {};
}
}

MyMap read_map() {
MyMap myMap;
while (true) {
auto entry = read_entry();
if (cin) {
myMap.insert(move(entry));
} else if (cin.eof()) {
return myMap;
} else {
throw invalid_argument("io error");
}
}
}

void dump_map(const MyMap &myMap) {
for (auto &&value : myMap) {
cout << value.first << "n"
<< value.second.first << "n"
<< value.second.second << endl;
}
}

int main() {
cout << "reading map..." << endl;
MyMap myMap;
try {
myMap = read_map();
} catch (invalid_argument e) {
cout << "error encountered reading map: " << e.what() << endl;
return 1;
}

cout << "dumping map..." << endl;
dump_map(myMap);
}





share|improve this answer
























  • Thank you very much for the example I will definitely look at it.

    – creation8383
    Nov 20 '18 at 7:16











  • No problem. If you find that this satisfies your original question, it would be appreciated if you mark your question as "answered" (click the checkmark).

    – Nathan Chappell
    Nov 20 '18 at 12:36











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53354978%2fhow-to-create-an-stdmap-and-save-it-by-using-the-console%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














I've put together some sample code to approximate your task. It reads some integers from standard input, and throws it back to standard output. For your purposes, you may wish to format the output differently, and read/save it from/to a file at some point. There is probably more terse, more robust, more efficient, and prettier ways to do it, but I put this together quickly to get you started. If you find things that are strange to you, I would be happy to answer some questions, but the idea of this site is that you prove that you put some work into finding out the answers first (for example by visiting other questions, reading some books, or consulting the C++ reference at: https://en.cppreference.com/w/cpp)



#include <iostream>
#include <map>
#include <stdexcept>

using namespace std;
using MyMap = map<int, pair<int, int>>;

MyMap::value_type read_entry() {
int key, v_1, v_2;
bool started_read{false};
if ((cin >> key) && (started_read = true) && (cin >> v_1) && (cin >> v_2)) {
return {key, {v_1, v_2}};
} else if (started_read) {
throw invalid_argument("improper input");
} else {
return {};
}
}

MyMap read_map() {
MyMap myMap;
while (true) {
auto entry = read_entry();
if (cin) {
myMap.insert(move(entry));
} else if (cin.eof()) {
return myMap;
} else {
throw invalid_argument("io error");
}
}
}

void dump_map(const MyMap &myMap) {
for (auto &&value : myMap) {
cout << value.first << "n"
<< value.second.first << "n"
<< value.second.second << endl;
}
}

int main() {
cout << "reading map..." << endl;
MyMap myMap;
try {
myMap = read_map();
} catch (invalid_argument e) {
cout << "error encountered reading map: " << e.what() << endl;
return 1;
}

cout << "dumping map..." << endl;
dump_map(myMap);
}





share|improve this answer
























  • Thank you very much for the example I will definitely look at it.

    – creation8383
    Nov 20 '18 at 7:16











  • No problem. If you find that this satisfies your original question, it would be appreciated if you mark your question as "answered" (click the checkmark).

    – Nathan Chappell
    Nov 20 '18 at 12:36
















0














I've put together some sample code to approximate your task. It reads some integers from standard input, and throws it back to standard output. For your purposes, you may wish to format the output differently, and read/save it from/to a file at some point. There is probably more terse, more robust, more efficient, and prettier ways to do it, but I put this together quickly to get you started. If you find things that are strange to you, I would be happy to answer some questions, but the idea of this site is that you prove that you put some work into finding out the answers first (for example by visiting other questions, reading some books, or consulting the C++ reference at: https://en.cppreference.com/w/cpp)



#include <iostream>
#include <map>
#include <stdexcept>

using namespace std;
using MyMap = map<int, pair<int, int>>;

MyMap::value_type read_entry() {
int key, v_1, v_2;
bool started_read{false};
if ((cin >> key) && (started_read = true) && (cin >> v_1) && (cin >> v_2)) {
return {key, {v_1, v_2}};
} else if (started_read) {
throw invalid_argument("improper input");
} else {
return {};
}
}

MyMap read_map() {
MyMap myMap;
while (true) {
auto entry = read_entry();
if (cin) {
myMap.insert(move(entry));
} else if (cin.eof()) {
return myMap;
} else {
throw invalid_argument("io error");
}
}
}

void dump_map(const MyMap &myMap) {
for (auto &&value : myMap) {
cout << value.first << "n"
<< value.second.first << "n"
<< value.second.second << endl;
}
}

int main() {
cout << "reading map..." << endl;
MyMap myMap;
try {
myMap = read_map();
} catch (invalid_argument e) {
cout << "error encountered reading map: " << e.what() << endl;
return 1;
}

cout << "dumping map..." << endl;
dump_map(myMap);
}





share|improve this answer
























  • Thank you very much for the example I will definitely look at it.

    – creation8383
    Nov 20 '18 at 7:16











  • No problem. If you find that this satisfies your original question, it would be appreciated if you mark your question as "answered" (click the checkmark).

    – Nathan Chappell
    Nov 20 '18 at 12:36














0












0








0







I've put together some sample code to approximate your task. It reads some integers from standard input, and throws it back to standard output. For your purposes, you may wish to format the output differently, and read/save it from/to a file at some point. There is probably more terse, more robust, more efficient, and prettier ways to do it, but I put this together quickly to get you started. If you find things that are strange to you, I would be happy to answer some questions, but the idea of this site is that you prove that you put some work into finding out the answers first (for example by visiting other questions, reading some books, or consulting the C++ reference at: https://en.cppreference.com/w/cpp)



#include <iostream>
#include <map>
#include <stdexcept>

using namespace std;
using MyMap = map<int, pair<int, int>>;

MyMap::value_type read_entry() {
int key, v_1, v_2;
bool started_read{false};
if ((cin >> key) && (started_read = true) && (cin >> v_1) && (cin >> v_2)) {
return {key, {v_1, v_2}};
} else if (started_read) {
throw invalid_argument("improper input");
} else {
return {};
}
}

MyMap read_map() {
MyMap myMap;
while (true) {
auto entry = read_entry();
if (cin) {
myMap.insert(move(entry));
} else if (cin.eof()) {
return myMap;
} else {
throw invalid_argument("io error");
}
}
}

void dump_map(const MyMap &myMap) {
for (auto &&value : myMap) {
cout << value.first << "n"
<< value.second.first << "n"
<< value.second.second << endl;
}
}

int main() {
cout << "reading map..." << endl;
MyMap myMap;
try {
myMap = read_map();
} catch (invalid_argument e) {
cout << "error encountered reading map: " << e.what() << endl;
return 1;
}

cout << "dumping map..." << endl;
dump_map(myMap);
}





share|improve this answer













I've put together some sample code to approximate your task. It reads some integers from standard input, and throws it back to standard output. For your purposes, you may wish to format the output differently, and read/save it from/to a file at some point. There is probably more terse, more robust, more efficient, and prettier ways to do it, but I put this together quickly to get you started. If you find things that are strange to you, I would be happy to answer some questions, but the idea of this site is that you prove that you put some work into finding out the answers first (for example by visiting other questions, reading some books, or consulting the C++ reference at: https://en.cppreference.com/w/cpp)



#include <iostream>
#include <map>
#include <stdexcept>

using namespace std;
using MyMap = map<int, pair<int, int>>;

MyMap::value_type read_entry() {
int key, v_1, v_2;
bool started_read{false};
if ((cin >> key) && (started_read = true) && (cin >> v_1) && (cin >> v_2)) {
return {key, {v_1, v_2}};
} else if (started_read) {
throw invalid_argument("improper input");
} else {
return {};
}
}

MyMap read_map() {
MyMap myMap;
while (true) {
auto entry = read_entry();
if (cin) {
myMap.insert(move(entry));
} else if (cin.eof()) {
return myMap;
} else {
throw invalid_argument("io error");
}
}
}

void dump_map(const MyMap &myMap) {
for (auto &&value : myMap) {
cout << value.first << "n"
<< value.second.first << "n"
<< value.second.second << endl;
}
}

int main() {
cout << "reading map..." << endl;
MyMap myMap;
try {
myMap = read_map();
} catch (invalid_argument e) {
cout << "error encountered reading map: " << e.what() << endl;
return 1;
}

cout << "dumping map..." << endl;
dump_map(myMap);
}






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 17 '18 at 20:49









Nathan ChappellNathan Chappell

1277




1277













  • Thank you very much for the example I will definitely look at it.

    – creation8383
    Nov 20 '18 at 7:16











  • No problem. If you find that this satisfies your original question, it would be appreciated if you mark your question as "answered" (click the checkmark).

    – Nathan Chappell
    Nov 20 '18 at 12:36



















  • Thank you very much for the example I will definitely look at it.

    – creation8383
    Nov 20 '18 at 7:16











  • No problem. If you find that this satisfies your original question, it would be appreciated if you mark your question as "answered" (click the checkmark).

    – Nathan Chappell
    Nov 20 '18 at 12:36

















Thank you very much for the example I will definitely look at it.

– creation8383
Nov 20 '18 at 7:16





Thank you very much for the example I will definitely look at it.

– creation8383
Nov 20 '18 at 7:16













No problem. If you find that this satisfies your original question, it would be appreciated if you mark your question as "answered" (click the checkmark).

– Nathan Chappell
Nov 20 '18 at 12:36





No problem. If you find that this satisfies your original question, it would be appreciated if you mark your question as "answered" (click the checkmark).

– Nathan Chappell
Nov 20 '18 at 12:36


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53354978%2fhow-to-create-an-stdmap-and-save-it-by-using-the-console%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

Tangent Lines Diagram Along Smooth Curve

Yusuf al-Mu'taman ibn Hud

Zucchini