C++ 使用 Vector Pair 读取数据类型 string int 和 double

C++ read data types string int and double using Vector Pair

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

 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

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

oil     
66
butter 
27.2
bread   
21.8

任何想法将不胜感激!

如果你只想使用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;
}

你 can/should 而不是使用 classstruct 而不是 std::pair.

可以看到上面程序的输出here。输入文件也附在上面link。上面版本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.