1

我正在尝试学习 C++ 并遇到了这个问题陈述

挑战说明:

我们的营销部门刚刚与几家当地商家谈判达成协议,这将使我们能够每天为我们的顶级客户提供各种产品的独家折扣。问题是我们只能向一位客户提供每种产品,并且我们只能向每位客户提供一种产品。

每天我们都会获得有资格享受这些特别折扣的产品列表。然后,我们必须决定向哪些客户提供哪些产品。幸运的是,我们的高技能统计学家团队开发了一个惊人的数学模型,通过计算我们所谓的“适用性分数”(SS)来确定给定客户购买所提供产品的可能性。计算客户和产品之间的 SS 的绝密算法是这样的:

1. 如果产品名称中的字母数是偶数,那么SS是客户名称中元音(a、e、i、o、u、y)的个数乘以1.5。
2. 如果产品名称中的字母数为奇数,则 SS 为客户名称中的辅音数。
3. 如果产品名称中的字母数与客户名称中的字母数有任何公因数(除 1 之外),则 SS 乘以 1.5。

您的任务是实施一个程序,该程序为每个客户分配要提供的产品,以最大化所有所选报价的组合总 SS。请注意,可能有不同数量的产品和客户。只要您引用源代码,您就可以包含来自外部库的代码。

输入样本:

您的程序应该接受文件路径作为其唯一参数。该文件中的每一行都是一个测试用例。每个测试用例将是一组以逗号分隔的客户名称,后跟一个分号,然后是一组以逗号分隔的产品名称。假设输入文件是 ASCII 编码的。例如(注意:下面的示例有 3 个测试用例):

Jack Abraham、John Evans、Ted Dziuba;iPad 2 - 4 件装,Girl Scouts Thin Mints,Nerf Crossbow

Jeffery Lebowski、Walter Sobchak、Theodore Donald Kerabatsos、Peter Gibbons、Michael Bolton、Samir Nagheenanajar;Half & Half、Colt M1911A1、16lb 保龄球、Red Swingline 订书机、打印纸、Vibe 杂志订阅 - 40 包

Jareau Wade,Rob Eroh,Mahmoud Abdelkader,Wenyi Cai,Justin Van Winkle,Gabriel Sinkin,Aaron Adelson;蝙蝠侠 1 号,足球 - 官方尺寸,低音放大耳机,大象食品 - 1024 磅,三狼一月 T 恤,唐培里侬 2000 年份葡萄酒

输出样本:

对于每一行输入,将最大总分打印到小数点后两位。对于上面的示例输入,输出应如下所示:

21.00
83.50
71.25

错误

在我的程序中,我创建了一个二维字符串向量

`std::vector<std::vector<std::string>`

我希望将输入文件中每一行的名称存储为第二维的字符串

假设二维向量的名称是a并且我有一个字符串“Dexobox Valektra” ,如果可以将向量视为数组 ,则需要将其推入位置a[0][0] 。

我尝试使用 push_back 的是a[i].push_back(token) ,它可以编译但在运行时导致分段错误。

我该如何解决?有没有不同的方法可以推入向量的第二维?

我正在使用例程将名称与逗号分隔符和 push_back 分隔到向量中

这是代码片段:

void comma_seprate(std::string s, std::vector < std::vector<std::string> >& a , int i)
{
std::istringstream ss(s);
std::string token;
int j = 0;
while(std::getline(ss, token, ','))
{
    a[i].push_back(token); //this is  the line causing segmentation fault at runtime
    std::cout << token << std::endl;
    j++;
    }
}

这是完整的代码:

#include <iostream>
#include <string>
#include <cstdlib>
#include <fstream>
#include <vector>
#include <sstream>

#define DEBUG

void comma_seprate(std::string s, std::vector < std::vector<std::string> >& a , int i)
{
    std::istringstream ss(s);
    std::string token;
    int j = 0;
    while(std::getline(ss, token, ','))
    {
        a[i].push_back(token);
        std::cout << token << std::endl;
        j++;
    }
}

void split_string( std::string s, std::vector<std::string>& v, std::vector<std::string>& u )
{
    std::string delimiter = ";";
    v.push_back (s.substr(0, s.find(delimiter)));
    u.push_back (s.substr(s.find(delimiter)+1,s.size()));
}

int main ( int argc, char** argv )
{
    //variable for file name
    std::string filename;   
    //error handling for invalid argument size      
    if ( argc > 2 || argc < 2 )
    {
        std::cerr <<"filename missing! Usage: " << argv[0] << " <input_filename>"<< std::endl;
        return EXIT_FAILURE;
    }
//=========================================================================     
    //Using C style debugging : Any other way to do this in c++?
    #ifdef DEBUG
    std::cout << "filename :"<<filename << "\t  num_arguments: " << argc << std::endl;
    #endif
//=========================================================================     
    //opening the file for reading
    std::ifstream in(argv[1]);
    std::vector<std::string> line_vec;
    std::string temp_line;
    int line_count = 0;
    while(std::getline(in,temp_line))
    {
        line_count++;
        //Ignores any empty lines in the input file
        if(temp_line.empty())   
            continue;
        line_vec.push_back(temp_line);
    }

    //=========================================================================     
    #ifdef DEBUG
            std::cout <<"\nPrinting out contents of the line_vec" <<std::endl;
            for (int i=0; i<line_vec.size();i++){
                std::cout << line_vec[i] << std::endl;
            }
            std::cout << "The size of the line_vector is : " << line_vec.size() << std::endl;
    #endif
    //=========================================================================

    //Now splitting line by semicolon for customer names and product name seperation
    std::vector<std::string> customer_list;
    std::vector<std::string> product_list;
    for (int i=0; i<line_vec.size();i++)
    {
        split_string(line_vec[i], customer_list, product_list);
    }

    #ifdef DEBUG
            std::cout <<"=======================================================================" <<std::endl;
            std::cout <<"\nPrinting out contents of the customer_list " <<std::endl;
            std::cout <<"=======================================================================" <<std::endl;
            for (int i=0; i<customer_list.size();i++){
                std::cout << customer_list[i] << "\n\n" << std::endl;
            }
            std::cout << "The size of the customer_list vector is : " << customer_list.size() << std::endl;
            std::cout <<"=======================================================================" <<std::endl;
            std::cout <<"\nPrinting out contents of the product_list " <<std::endl;
            std::cout <<"=======================================================================" <<std::endl;
            for (int i=0; i<product_list.size();i++){
                std::cout << product_list[i] << "\n\n" << std::endl;
            }
            std::cout << "The size of the line_vector vector is : " << product_list.size() << std::endl;
    #endif

    //comma seprating the sting to get a list of customer names and product names
    std::vector < std::vector < std::string > > customer_name;
    std::vector < std::vector < std::string > > product_name;

    for(int i =0; i< customer_list.size(); i++)
    {
        comma_seprate(customer_list[i],customer_name,i);
        //comma_seprate(product_list[i],product_name,i);    
    }
}
4

1 回答 1

0

这意味着其中a[i]没有任何内容并且超出了范围。

你有一个向量的向量

std::vector < std::vector<std::string> >& a

所以你在第二个向量内推回元素

std::vector < std::vector<std::string> >& a
           // ^^^^^^^^^^^^^^^^^^^^^^^^ Inside this one here

但另一个向量它仍然是空的

 std::vector < std::vector<std::string> >& a
//^^^^^^^^^^^  that one is empty

因此,如果您确实a[i]崩溃了,这是正常的……因为它是空的。

您可以做的是首先创建内部向量。

while(std::getline(ss, token, ','))
{
   std::vector<std::string> inner_vector;
   inner_vector.push_back(token);
   a.push_back(inner_vector); //this is  the line causing segmentation fault at runtime
   std::cout << token << std::endl;
   j++;
}
于 2014-03-22T02:27:24.537 回答