Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/154.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 在向量中循环时发出<;字符串>;,并保留";计数“;每个元素的_C++_C++11 - Fatal编程技术网

C++ 在向量中循环时发出<;字符串>;,并保留";计数“;每个元素的

C++ 在向量中循环时发出<;字符串>;,并保留";计数“;每个元素的,c++,c++11,C++,C++11,首先,这是我在网站上的第一个问题。我已经做了很多研究,我不认为我发现了这样一个非常具体的问题,但如果我错了,请随意在回答中纠正我,并将所述主题链接到我 对于问题本身,分配由一个控制台应用程序组成,该应用程序将显示输入的每个不同单词,以及每个唯一单词的出现次数。我决定解决这个问题的方法是使用向量,然后使用嵌套循环结构,其中外循环表示每个唯一的单词,内循环用于将外循环中的单词与向量中的每个现有单词进行比较 不过。我遇到了一个问题 使用此基本设置: //Sort vector into alphabe

首先,这是我在网站上的第一个问题。我已经做了很多研究,我不认为我发现了这样一个非常具体的问题,但如果我错了,请随意在回答中纠正我,并将所述主题链接到我

对于问题本身,分配由一个控制台应用程序组成,该应用程序将显示输入的每个不同单词,以及每个唯一单词的出现次数。我决定解决这个问题的方法是使用
向量
,然后使用嵌套循环结构,其中外循环表示每个唯一的单词,内循环用于将外循环中的单词与向量中的每个现有单词进行比较

不过。我遇到了一个问题

使用此基本设置:

//Sort vector into alphabetical order
sort(words.begin(), words.end()); //this only sorts them alphabetically, but equal strings are "next" to each other

//Find unique values
for(string::size_type i=0; i != words.size(); i++) {
    int count = 0;
    for(string::size_type j=0; j != words.size(); j++) {
        if(words[i] == words[j]){
            count++;
        }
    }
    cout << words[i] << " appeared: " << count << " times." << endl;
}
//将向量按字母顺序排序
排序(words.begin(),words.end())//这仅按字母顺序对它们进行排序,但相等的字符串彼此“相邻”
//找到唯一的值
for(string::size_type i=0;i!=words.size();i++){
整数计数=0;
对于(字符串::size_type j=0;j!=words.size();j++){
如果(字[i]==字[j]){
计数++;
}
}

我想你应该检查一下我!=j;如果它与自己比较

//Find unique values
for(string::size_type i=0; i != words.size(); i++) {
int count = 0;
for(string::size_type j=0; j != words.size(); j++) {
    if(words[i] == words[j]&&i!=j){
        count++;
    }
}
cout << words[i] << " appeared: " << count << " times." << endl;
}
//查找唯一值
for(string::size_type i=0;i!=words.size();i++){
整数计数=0;
对于(字符串::size_type j=0;j!=words.size();j++){
如果(字[i]==字[j]&&i!=j){
计数++;
}
}

cout使用称为哈希表的数据结构可以很容易地解决这个问题。哈希表是一个包含键值对的关联数组。基本上,“键”可以是一个字,用于计算数组的索引,其中“值”在你的例子中,它可以是被计数的次数。C++有< /P>
std::unordered_map
这是一个哈希表。请看一下哈希表背后的理论: 请看这里的C++版本:
这将使您的程序更易于编写。当输入值为1时,您可以将单词添加到哈希表中。当您再次看到单词时,增加其关联值。

您的代码是正确的,您只需检查
让我们看看您的示例案例失败的地方:

for(string::size_type j=0; j != words.size(); j++) { // i: 1, j: 2, size(words): 3
    if(words[i] == words[j]){ // words[i] matches words[j]
        count++;
        if(i != j){ // i doesn't match j
            words.erase(words.begin() + j); // i: 1, j: 2, size(words): 2
        }
    }
} // Upon rexecuting the iteration expression i: 1, j: 3, size(words): 2 thus `j` will be greater than `size(words)` and will be used to continue the loop even though it is an invalid index
使用您当前的代码,有几种解决方案可以解决此问题。但我建议,解决此问题的最简单方法是:

const多集字{istream_迭代器(cin),istream_迭代器();
自动it=cbegin(文字);
while(it!=cend(words)){
自动i=字。上界(*it);

cout更新:


将运算符
!=
简单更改为
可以考虑改用:
std::无序映射我的映射;
。然后可以添加如下字符串:
++my映射[my\u string];
。字符串将是键,值将是所述字符串键的出现次数。您擦除元素
j
,然后立即递增
j
,跳过替换已擦除元素的元素,并可能访问向量的末尾(因为在循环中使用相等比较).Bad.user2296177指出,哈希映射是解决此问题的一种极好的数据结构。如果要找出某个随机字符串的计数,则必须解析整个向量(或直到该字符串出现在向量中),该向量将为O(n),但它将为O(1)在散列图中进行操作。感谢输入人员!VadaPoché和user2296177,我将确保在深入了解语言时进一步了解这一点,1201程序,你是对的,我觉得这也是问题所在,但我无法找到解决方案。这只是循环条件,但有点尴尬。谢谢!谢谢你的详细解释,但我正在处理这些问题,因为它们是基于我购买的教科书《加速C++》,可惜它没有提供“原型”问题的答案。我有一种感觉,就是尺寸被修改是问题所在,谢天谢地,将
!=
更改为
更新:对于单词重复2+次的情况,简单的比较更改是不够的,即“测试”3次将被报告为“测试出现2次”,然后“测试出现1次”,但我怀疑这是因为“破坏性”再次从
erase
方法接近。在测试了您的方法后,我必须承认,虽然它超出了我目前的知识范围,但它是解决问题的更准确的方法,因此我将勾选的答案改为此。谢谢!@M.Lago查看我对我的答案所做的编辑,它现在为您解决了问题“单词重复2次”的例子。“AHMMEDAKHTAR好的眼睛,这也会起作用。我想出了一个使用重复向量的解决方案,但是我认为这样的方法更有效,因此更干净/更快的解决方案。谢谢分享!我已经注意到了上面的另一个用户,我正在跟随我的文本“加速C++”。“。当我觉得我已经对其他元素进行了足够的练习时,我也会将此书签保留下来,但感谢您提供的参考和详细解释。感谢您的帮助,但如上所述,更简单的修复方法是更正循环条件以使用

//Sort vector into alphabetical order
sort(words.begin(), words.end()); //this only sorts them alphabetically, but equal strings are "next" to each other

//Find unique values
for(string::size_type i=0; i < words.size(); i++) {
    int count = 0;
    for(string::size_type j=0; j < words.size(); j++) {
        if(words[i] == words[j]){
            count++;
            if(i != j){ //replacement: delete duplicate values from the vector (aka if the indexes don't match)
                words.erase(words.begin() + j); //delete element at index "j"
                j--; // Re-run iteration for j
            }
        }
    }
    cout << words[i] << " appeared: " << count << " times." << endl;
}
for(string::size_type j=0; j != words.size(); j++) { // i: 1, j: 2, size(words): 3
    if(words[i] == words[j]){ // words[i] matches words[j]
        count++;
        if(i != j){ // i doesn't match j
            words.erase(words.begin() + j); // i: 1, j: 2, size(words): 2
        }
    }
} // Upon rexecuting the iteration expression i: 1, j: 3, size(words): 2 thus `j` will be greater than `size(words)` and will be used to continue the loop even though it is an invalid index
const multiset<string> words{istream_iterator<string>(cin), istream_iterator<string>()};
auto it = cbegin(words);

while(it != cend(words)) {
    auto i = words.upper_bound(*it);

    cout << *it << " appeared: " << distance(it, i) << " times\n";
    it = i;
}
//Sort vector into alphabetical order
sort(words.begin(), words.end()); //this only sorts them alphabetically, but equal strings are "next" to each other

//Find unique values
for(string::size_type i=0; i < words.size(); i++) {
    int count = 0;
    //duplicate vector, and use it for the inner loop
    vector<string> duplicate = words;
    for(string::size_type j=0; j < duplicate.size(); j++) {
        if(words[i] == words[j]){
            count++;
            if(i != j){ //replacement: delete duplicate values from the vector (aka if the indexes don't match)
                words.erase(words.begin() + j); //delete element at index "j"
            }
        }
    }
    cout << words[i] << " appeared: " << count << " times." << endl;
}