c++ multiple cin 如果没有输入默认值,添加到一个map<string, vector<double>>
c++ multiple cin default value if not entered, adding to a map<string, vector<double>>
任务是这样的:
您应该在一行中输入名称、价格、金额,如果未给出金额,则金额应默认为 1。我的问题是:
如果您输入以下内容,您应该输入任意多行:
猴子 200 3
黄貂鱼 399 1
它有效,但如果您只提及名称和价格,则应将金额添加到地图中,如果未提供任何金额,则金额为 1。而是 ut 跳转到下一个,你在 int 上输入一个字符串名称,循环崩溃。还有其他方法吗?
#include <string>
#include <map>
#include <vector>
int main(){
map<string, vector<double>> vec;
string name;
double price;
double amount;
while(cin >> name >> price >> amount){
vec[name] = {price , amount}
}
return 0;
}
试试这个:
#include <string>
#include <map>
#include <vector>
#include <cctype>
int main(){
map<string, vector<double>> vec;
string name;
double price;
double amount;
int peek_;
do{
amount = 1;
std::cin >> name >> price;
std::cin >> std::ws; //get rid of white space
int peek_ = cin.peek();
if (std::isdigit(peek_)){
std::cin >> amount;
}
vec[name] = {price , amount}
} while (peek_ != EOF)
return 0;
}
这应该给您大概的想法。另请查看 here 了解更多信息。
注意:我是在旅途中写的,所以没有编译或测试。您可能需要稍微玩一下语法 :)
希望对您有所帮助。
我解决了我自己的问题。这是我的解决方案,它适用于我在问题中提到的参数。
Firstclass myclass;
string item, data;
vector<string> split_input;
// reads in on line of string until ctrl-z/ctrl-d
while(getline(cin, data)){
stringstream str_stream(data);
// reading the values separate adding them to vector
while(str_stream >> item{
split_input.push_back(item);
}
// if amount is not given
if(v.size() == 2){
myclass.insert_data(split_input[0], stod(split_input[1]), 1.00);
}
// if if amount is given
else{
myclass.insert_data(split_input[0], stod(split_input[1]), stod(split_input[2]));
}
// clearing the vector
split_input.clear();
}
任务是这样的: 您应该在一行中输入名称、价格、金额,如果未给出金额,则金额应默认为 1。我的问题是: 如果您输入以下内容,您应该输入任意多行: 猴子 200 3 黄貂鱼 399 1
它有效,但如果您只提及名称和价格,则应将金额添加到地图中,如果未提供任何金额,则金额为 1。而是 ut 跳转到下一个,你在 int 上输入一个字符串名称,循环崩溃。还有其他方法吗?
#include <string>
#include <map>
#include <vector>
int main(){
map<string, vector<double>> vec;
string name;
double price;
double amount;
while(cin >> name >> price >> amount){
vec[name] = {price , amount}
}
return 0;
}
试试这个:
#include <string>
#include <map>
#include <vector>
#include <cctype>
int main(){
map<string, vector<double>> vec;
string name;
double price;
double amount;
int peek_;
do{
amount = 1;
std::cin >> name >> price;
std::cin >> std::ws; //get rid of white space
int peek_ = cin.peek();
if (std::isdigit(peek_)){
std::cin >> amount;
}
vec[name] = {price , amount}
} while (peek_ != EOF)
return 0;
}
这应该给您大概的想法。另请查看 here 了解更多信息。
注意:我是在旅途中写的,所以没有编译或测试。您可能需要稍微玩一下语法 :)
希望对您有所帮助。
我解决了我自己的问题。这是我的解决方案,它适用于我在问题中提到的参数。
Firstclass myclass;
string item, data;
vector<string> split_input;
// reads in on line of string until ctrl-z/ctrl-d
while(getline(cin, data)){
stringstream str_stream(data);
// reading the values separate adding them to vector
while(str_stream >> item{
split_input.push_back(item);
}
// if amount is not given
if(v.size() == 2){
myclass.insert_data(split_input[0], stod(split_input[1]), 1.00);
}
// if if amount is given
else{
myclass.insert_data(split_input[0], stod(split_input[1]), stod(split_input[2]));
}
// clearing the vector
split_input.clear();
}