首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >二维向量上通过for循环的Push_back

二维向量上通过for循环的Push_back
EN

Stack Overflow用户
提问于 2014-03-22 02:12:18
回答 1查看 3.5K关注 0票数 1

我正在努力学习C++,并偶然发现了这个问题陈述。

挑战说明:

我们的市场部刚刚与当地的几家商家达成了一项协议,允许我们每天向我们的顶级客户提供各种产品的独家折扣。问题是,我们只能向一个客户提供每种产品,我们也只能向每个客户提供一种产品。

每天我们都会收到符合这些特殊折扣条件的产品清单。然后,我们必须决定哪些产品提供给我们的客户。幸运的是,我们的高技能统计人员团队开发了一个惊人的数学模型,通过计算我们所说的“适合性评分”(SS)来确定给定客户购买所提供产品的可能性。计算客户和产品之间的SS的绝密算法如下:

1.如果产品名称中的字母数为偶数,则SS为客户名称中元音数(a、e、i、o、u、y)乘以1.5。

2.如果产品名称中的字母数为奇数,则SS为客户名称中辅音的数目。

3.如果产品名称中的字母数与客户姓名中的字母数有任何共同因素(除1外),则SS乘以1.5。

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

输入样本:

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

杰克·亚伯拉罕,约翰·埃文斯,泰德·迪齐巴;iPad 2-4包,女童子军薄荷糖,Nerf十字弓

Jeffery Lebowski,Walter Sobchak,Theodore Donald Kerabatsos,Peter Gibbons,Michael Bolton,Samir Nagheenanajar;半人和半人,Colt M1911A1,16磅保龄球,红色斯普林斯,打印机纸张,Vibe杂志订阅- 40包

贾劳·韦德,罗布·埃罗,马哈茂德·阿卜杜勒卡德尔,蔡文义,贾斯汀·范·温克尔,加布里埃尔·辛金,亚伦·阿德尔森;蝙蝠侠一号,足球官方尺寸,巴斯放大耳机,大象食物- 1024磅,三件狼一号月亮T恤,Dom Perignon 2000年份

输出样本:

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

21.00

83.50

71.25

误差

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

代码语言:javascript
复制
`std::vector<std::vector<std::string>`

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

让我们假设2D向量的名称是a,而我有一个字符串"Dexobox“,如果可以将向量作为数组处理,则需要在location 中推送

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

我该怎么解决这个问题?是否有不同的方式推进到向量的第二维?

我使用一个例程将带有逗号、分隔符和push_back的名称分隔到向量中。

下面是代码片段:

代码语言:javascript
复制
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++;
    }
}

以下是完整的代码:

代码语言:javascript
复制
#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);    
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-03-22 02:27:24

这意味着a[i]里面什么都没有,而且超出了。

你有一个向量向量

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

所以你把第二个向量中的元素向后推

代码语言:javascript
复制
std::vector < std::vector<std::string> >& a
           // ^^^^^^^^^^^^^^^^^^^^^^^^ Inside this one here

但另一个向量仍然是空的

代码语言:javascript
复制
 std::vector < std::vector<std::string> >& a
//^^^^^^^^^^^  that one is empty

所以如果你做a[i]崩溃是正常的..。因为它是空的。

你能做的是,首先创建内部向量。

代码语言:javascript
复制
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++;
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22572587

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档