【问题标题】:C++ read data types string int and double using Vector PairC++ 使用向量对读取数据类型字符串 int 和 double
【发布时间】:2021-12-20 14:23:15
【问题描述】:

问题是我必须读取包含以下内容的文件:

 type     count    price

bread      10       1.2 
butter      6       3.5
bread       5       1.3
oil        20       3.3
butter      2       3.1
bread       3       1.1

我必须使用向量对来读取文件并将计数和价格相乘,输出应该是:

oil     
66
butter 
27.2
bread   
21.8

任何想法将不胜感激!

【问题讨论】:

  • 你试过什么?你在哪里卡住了?
  • std::pair 听起来像是包含 3 个字段的数据的错误类型。
  • 我不会将它用于世界,但我的老师要求我们使用向量对。

标签: c++ string vector integer double


【解决方案1】:

如果您只想使用std::pairstd::vector,那么您可以使用以下程序作为起点(参考):

版本 1:产品名称会重复

#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
int main()
{
    std::ifstream inputFile("input.txt"); //open the file
    std::string line; 
    
    std::vector<std::pair<std::string, double>> vec;
    
    std::string name;
    double price, count;
    
    if(inputFile)
    {   std::getline(inputFile, line, '\n');//read the first line and discard it
        while(std::getline(inputFile, line, '\n'))//read the remaining lines
        {
            std::istringstream ss(line);
            ss >> name; //read the name of the product into variable name
            ss >> count;//read the count of the product into variable count
            ss >> price;//read the price of the product into variable price
            
            vec.push_back(std::make_pair(name, count * price));
        }
    }
    else 
    {
        std::cout<<"File cannot be opened"<<std::endl;
    }
    inputFile.close();
    
    //lets print out the details 
    for(const std::pair<std::string, double> &elem: vec)
    {
        std::cout<< elem.first<< ": "<< elem.second<<std::endl;
    }
    return 0;
}

您可以/应该使用classstruct 而不是使用std::pair

上面程序的输出可以看到here。输入文件也附在上面的链接中。以上版本1的输出为:

bread: 12
butter: 21
bread: 6.5
oil: 66
butter: 6.2
bread: 3.3

正如您在版本 1 的输出中看到的那样,产品的名称是重复的。如果您不想要重复的名称并且想要总结与重复键相对应的值,请查看下面给出的版本 2:

第 2 版:产品名称不重复

#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
int findKey(const std::vector<std::pair<std::string, double>> &vec, const std::string &key)
{   int index = 0;
    for(const std::pair<std::string, double> &myPair: vec)
    {
        if(myPair.first == key)
        {
            return index;
        }
        ++index;
    }
    return -1;//this return value means the key was not already in the vector 
}
int main()
{
    std::ifstream inputFile("input.txt");
    std::string line; 
    
    std::vector<std::pair<std::string, double>> vec;
    
    std::string name;
    double price, count;
    
    if(inputFile)
    {   std::getline(inputFile, line, '\n');
        while(std::getline(inputFile, line, '\n'))
        {
            std::istringstream ss(line);
            ss >> name; 
            ss >> count;
            ss >> price;
            int index = findKey(vec, name);
            if(index == -1)
            {
                vec.push_back(std::make_pair(name, count * price));    
            }
            else 
            {
                vec.at(index).second += (count *price);
            }
            
        }
    }
    else 
    {
        std::cout<<"File cannot be opened"<<std::endl;
    }
    inputFile.close();
    
    //lets print out the details 
    for(const std::pair<std::string, double> &elem: vec)
    {
        std::cout<< elem.first<< ": "<< elem.second<<std::endl;
    }
    return 0;
}

版本2的输出是

bread: 21.8
butter: 27.2
oil: 66

可以看到here

【讨论】:

  • 太好了,非常感谢。我还有一些事情要做,但总的来说这是主要问题。再次感谢你。一切顺利,保重。
  • @ДенисПетков 不客气。如果对您有帮助,您能否将此答案标记为正确?
  • 当然,但它会打印出面包、黄油和油的第一个元素。问题是它删除了其他重复项而不添加它们的值。输出应该是:油 66 ||黄油 27.2 ||面包 21.8。知道如何删除重复项但计算它们的键值吗?
  • @ДенисПетков 我在上面添加了我的程序的版本 2,它不会显示重复项,但仍可以根据需要计算它们的值。看看吧。
  • 太棒了,感谢V2。我很快就会看看!保重。
猜你喜欢
  • 2014-12-04
  • 2022-01-16
  • 2021-11-13
  • 1970-01-01
  • 1970-01-01
  • 2020-09-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多