c++ - 循环永无止境??~需要帮助。 C++

标签 c++ algorithm

问题:

给定这样的输入:

int myintsA[]={1,3,3,2,2,5,5,5,4};
int myintsB[]={0,9,8,6,7,3,3,4};

找到相同的元素,输出。当它们同时出现 2 次时。输出为 2 倍。

例如:
输出: {3,3,4}

   #include <iostream>
#include <vector>
#include <list>
#include <ext/hash_map>

using namespace __gnu_cxx;
using namespace std;

list<int> findDup(const vector<int>& A ,const vector<int>& B)
{
    list<int> idx;
    std::vector<int>::size_type i = 0;
    std::vector<int>::size_type j = 0;
    while(i < A.size() && j < B.size()) {
        if (A[i] == B[j])
        {
            idx.push_back(A[i]);
            i++;
            j++;
        }
        else if(A[i] < B[j])
        {
            i++;
        }
        else if (A[i] > B[j])
        {
            j++;
        }
    }
    return idx;

}

int main()
{
    int myintsA[]={1,3,2,2,5,5,5,4};
    int myintsB[]={0,9,8,6,7,3,3,4};

    vector<int> myvectorA (myintsA, myintsA + sizeof(myintsA) / sizeof(int) );
    vector<int> myvectorB (myintsB, myintsB + sizeof(myintsB) / sizeof(int) );


    sort(myvectorA.begin(),myvectorA.end());
    sort(myvectorB.begin(),myvectorB.end());

    list<int> result = findDup(myvectorA, myvectorB);
    for(list<int>::iterator iter = result.begin(); iter!=result.end();++iter)
    {
        printf("%c",*iter);
    }
    return 0;
}

但是我的程序好像有问题。需要帮忙! 谢谢!

最佳答案

这是之前发布的代码的工作版本:

#include <iostream>
#include <vector>
#include <list>
#include <algorithm> //It's better to use the standard <algorithm> header here for sort.
                     //using <ext/hash_map> just for the sort functionality is not a great idea because it makes the code less clear and also creates portability issues.
using namespace __gnu_cxx;
using namespace std;

list<int> findDup(const vector<int>& A ,const vector<int>& B)
{
    list<int> idx;
    std::vector<int>::size_type i = 0;
    std::vector<int>::size_type j = 0;
    while(i < A.size() && j < B.size()) { //as pointed out before this is the source of the error
        if (A.at(i) == B.at(j)) 
        //using the .at(i) will throw an exception if anything goes out of range of the container. 
        //Operator [] doesn't provide this safety.
        {
            idx.push_back(A.at(i));
            i++;
            j++;
        }
        else if(A.at(i) < B.at(j))
        {
            i++;
        }
        else if (A.at(i) > B.at(j))
        {
            j++;
        }
    }

    return idx; 
    //you didn't actually return anything before

}

int main()
{
    int myintsA[]={1,3,3,2,2,5,5,5,4};
    int myintsB[]={0,9,8,6,7,3,3,4};

    vector<int> myvectorA (myintsA, myintsA + sizeof(myintsA) / sizeof(int) );
    vector<int> myvectorB (myintsB, myintsB + sizeof(myintsB) / sizeof(int) );


    sort(myvectorA.begin(),myvectorA.end());
    sort(myvectorB.begin(),myvectorB.end());

    list<int> result = findDup(myvectorA, myvectorB);
    for(list<int>::iterator iter = result.begin(); iter!=result.end();++iter)
    {
        cout<< *iter ; 
        //using cout is generally safer and more idiomatic c++
    }
            cout << endl;

    return 0;
}

主要问题是最后发生的边缘情况。 有几点需要注意: 如果您使用 .at(i) 语法,您会抛出一个 std::out_of_range ,这会为您指出正确的方向来发现问题。此外,如果您使用 -Wall 进行编译,您会收到关于第一个函数未返回任何内容的警告。

关于c++ - 循环永无止境??~需要帮助。 C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5131694/

相关文章:

c++ - 使用 msgpack-c 时需要清理什么以及何时需要清理?

c++ - 有没有更好的方法从静态和非静态函数返回相同的字符串文字?

c++ - 如何更新对象的属性?

algorithm - 你怎么称呼迭代洪水填充算法?

algorithm - 最大化整数数组中的熵

C++ 模板继承

c++ - 打印所有字符串,O(2^n) 算法

c++ - 测试树是否为二叉搜索树

java - 编写代码以仅使用一次 isSubstring 调用来检查 s2 是否是 s1 的旋转

algorithm - 时间复杂度(递归)【前序遍历的BST构造】