没有set_intersection或set_difference的C ++中的矢量比较

时间:2018-03-22 16:05:58

标签: c++ vector comparison

只是一个学习C ++的新手。这是一门很好的语言,有着陡峭的学习曲线,我正在爬山。

目前,我想比较两个向量,第一个向量已经有预定义的元素,而另一个向量的元素是在go上输入的(即“while循环中的cin”)。每当在两个排序的向量之间匹配相似的字符串时,向量比较的输出应该是字符串“bleep”。否则,带有输入单词的向量将打印出它的元素。

旁注:代码有点宗教(我决定使用我知道的标准文本 - 圣经)。因此,我并不是要谴责任何我只需要一个我们都可以访问的标准文本的人。此外,我已经在线搜索了一些比较向量所需的建议,我使用了set_difference,set_intersection等。我已经检查了它们,但我意识到需要像我这样的新手有更多的时间来理解interator语法。所以这就是我在这里的原因。

所以,我需要你的帮助,因为代码不断从实现文件中提出这个错误: enter image description here

我知道。我希望我能解决错误(错误实际上是我正在阅读的书中下一章的主题)

这里有足够的小谈,就是我一直在努力的代码。任何指针将不胜感激。在此先感谢:)

#include "stdafx.h"
#include "Std_lib_facilities.h"
int main()
{
    // Introduction

    cout << "This program will compare the your input to already predefined works which are found in Galatians 5:19-24.";

    // Vectors of disliked by God in Galatians 5:19-24. These are the works of the flesh. And I copied them from the Bible.
    // Side comment:I know we are all sinners- I am not judging anyone. I  myself am working on some with the help of the Holy Spirit and my spirit is getting stronger everyday.

    vector <string> disliked; disliked[0]= "hatred"; disliked[1] = "envy"; disliked[2] = "adultery"; disliked[3] = "fornication"; disliked[4] = "anger"; disliked[5] = "malice"; 
    disliked[6] = "gluttony"; disliked[7] = "lying"; disliked[8] = "stealing"; disliked[9] = "greed"; disliked[10] = "uncleanliness"; disliked[11] = "lewdness"; disliked[12] = "idolatry";
    disliked[13] = "sorcery"; disliked[14] = "contentions"; disliked[15] = "jealousy"; disliked[16] = "selfish ambition"; disliked[17] = "dissensions"; disliked[18] = "heresies";
    disliked[19] = "murder"; disliked[20] = "drunkeness"; disliked[21] = "revelries";

    // Sorted the vectors so that they could be compared with what are liked.
    sort(disliked.begin(), disliked.end());

    cout << "Please enter the fruits of the Holy Spirit in Galatians 5:22-23, while purposely adding some of the works of the flesh which are found in Galatians 5:19-24: \n" <<
        "Also inset a Ctrl+Z and click enter when you have finished inserting the works"<< endl;

    string first_input; // String for the input of the likes. Please, you are to purposely insert some words that are not listed in Galatians 5:22-23. Rather include some from Galatians 5:19-24
    vector <string> liked; // Vector of liked qualities

    while (cin >> first_input) // cin iostream is used to insert the whitespace separated strings into first_input
        liked.push_back(first_input); // push_back inserts the strings from first_input into the vector liked
        sort(liked.begin(), liked.end()); // the vector liked is sorted.

        for (int i = 0; i < liked.size(); ++i) // Using the loop for the comparison of the sorted vectors in the next if statment
            if (disliked == liked) // if strings are similar give a bleep!
                cout << "Bleep!" << endl;
            else
                cout << liked[i]; // if strings are not similar cout the list of liked qualities.


}

1 个答案:

答案 0 :(得分:1)

您的向量disliked长度为0个元素,因此您将分配给不存在的元素。

std::vector::operator[]不会改变容器的大小;它只是获取对已存在的元素的引用。要将元素添加到矢量的末尾,您需要使用push_back

vector<string> disliked;
disliked.push_back("hatred");
...

或者,您可以使用一些默认构造的元素创建它并分配给它们:

vector<string> disliked(22);
disliked[0] = "hatred";
...

由于您在构建时知道disliked的所有元素,因此可以使用列表初始化:

vector<string> disliked = {
    "hatred",
    ...
};