【问题标题】:Cannot get min_element to work in C++无法让 min_element 在 C++ 中工作
【发布时间】:2013-10-26 12:14:36
【问题描述】:

我正在编写一个小型递归算法。代码如下:

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

vector<int> coins;

int checkchange(int left) {
    vector<int> choices (coins.size());
    if (left == 0)
        return 0;
    else {
        int min;
        for (int i=0;i<coins.size();i++) {
            choices.at(i) = (1 + checkchange(left - coins.at(i)));
        }
        return min_element(choices.front(),choices.back());
    }
}


int main() {
    int N;
    cin >> N;
    for (int i=0;i<N;i++) {
        int c,m,temp,change;
        cin >> c >> m;
        for (int j=0;j<c;j++) {
            cin >> temp;
            coins.push_back(temp);
        }

        for (int j=0;j<m;j++) {
            cin >> temp;
            change = checkchange(temp);
            cout << change;
        }
    }
    return 0;
}

我收到以下错误:


中包含的文件中 /usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/algorithm:62,
从burningcoins.cpp:3:/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/stl_algo.h:
在函数'_FIter std::min_element(_FIter, _FIter) [with _FIter =
int]':burningcoins.cpp:19:从这里实例化
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/stl_algo.h:5998:
错误:“一元 *”的类型参数无效
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/stl_algo.h:5998:
错误:“一元 *”的类型参数无效

我试过用 g++ 和 gcc 编译,都给我同样的错误。我做错了什么?

编辑:

新代码:

int checkchange(int left) {
    vector<int> choices (coins.size());
    if (left == 0)
        return 0;
    else {
        for (int i=0;i<coins.size();i++) {
            choices[i] = (1 + checkchange(left - coins.at(i)));
        }
        return *min_element(choices.begin(), choices.end());
    }
}

新的错误信息:

/tmp/ccV3VLsK.o:在函数main': <br/> burningcoins.cpp:(.text+0x16a): undefined reference tostd::cin'
Burningcoins.cpp:(.text+0x16f): 未定义对std::basic_istream<char, std::char_traits<char> >::operator>>(int&)' <br/> burningcoins.cpp:(.text+0x187): undefined reference tostd::cin'
的引用 Burningcoins.cpp:(.text+0x18c): 未定义引用std::basic_istream<char, std::char_traits<char> >::operator>>(int&)' <br/> burningcoins.cpp:(.text+0x19b): undefined reference tostd::basic_istream >::operator>>(int&)'
burncoins.cpp:(.text+0x1b0): 未定义引用std::cin' <br/> burningcoins.cpp:(.text+0x1b5): undefined reference tostd::basic_istream >::operator>>(int&)'
Burningcoins.cpp:(.text+0x1ec): 未定义引用 std::cin' <br/> burningcoins.cpp:(.text+0x1f1): undefined reference tostd::basic_istream >::operator>>(int&)'
Burningcoins.cpp:(.text+0x208): 未定义引用std::cout' <br/> burningcoins.cpp:(.text+0x20d): undefined reference tostd::basic_ostream >::operator /tmp/ccV3VLsK.o:在函数__static_initialization_and_destruction_0(int, int)': <br/> burningcoins.cpp:(.text+0x261): undefined reference tostd::ios_base::Init::Init()'
burncoins.cpp:(.text+0x266): undefined reference to std::ios_base::Init::~Init()' <br/> /tmp/ccV3VLsK.o: In functionstd::vector >::_M_range_check(unsigned long) const':
burncoins.cpp:(.text._ZNKSt6vectorIiSaIiEE14_M_range_checkEm[std::vector >::_M_range_check(unsigned long) const]+0x2d): 未定义引用std::__throw_out_of_range(char const*)' <br/> /tmp/ccV3VLsK.o: In functionstd::vector >::_M_insert_aux(__gnu_cxx::__normal_iterator >>, int const&)':
burncoins.cpp:(.text._ZNSt6vectorIiSaIiEE13_M_insert_auxEN9__gnu_cxx17__normal_iteratorIPiS1_EERKi[std::vector >::_M_insert_aux(__gnu_cxx::__normal_iterator >>, int const&)]+0x259): 未定义引用__cxa_begin_catch' <br/> burningcoins.cpp:(.text._ZNSt6vectorIiSaIiEE13_M_insert_auxEN9__gnu_cxx17__normal_iteratorIPiS1_EERKi[std::vector<int, std::allocator<int> >::_M_insert_aux(__gnu_cxx::__normal_iterator<int*, std::vector<int, std::allocator<int> > >, int const&)]+0x2be): undefined reference to__'cxa_rethrow burncoins.cpp:(.text._ZNSt6vectorIiSaIiEE13_M_insert_auxEN9__gnu_cxx17__normal_iteratorIPiS1_EERKi[std::vector >::_M_insert_aux(__gnu_cxx::__normal_iterator > >, int const&)]+0x2c8): 未定义引用__cxa_end_catch' <br/> /tmp/ccV3VLsK.o: In functionstd::vector >:: long, char const*) const':
burncoins.cpp:(.text._ZNKSt6vectorIiSaIiEE12_M_check_lenEmPKc[std::vector >::_M_check_len(unsigned long, char const*) const]+0x4c): 未定义引用std::__throw_length_error(char const*)' <br/> /tmp/ccV3VLsK.o: In function__gnu_cxx::new_allocator::deallocate(int*, unsigned长)':
burncoins.cpp:(.text._ZN9__gnu_cxx13new_allocatorIiE10deallocateEPim[__gnu_cxx::new_allocator::deallocate(int*, unsigned long)]+0x1c): 未定义引用operator delete(void*)' <br/> /tmp/ccV3VLsK.o: In function__gnu_cxx::new_allocator::allocate(unsigned long, void const*) ':
burncoins.cpp:(.text._ZN9__gnu_cxx13new_allocatorIiE8allocateEmPKv[__gnu_cxx::new_allocator::allocate(unsigned long, void const*)]+0x35): 未定义引用std::__throw_bad_alloc()' <br/> burningcoins.cpp:(.text._ZN9__gnu_cxx13new_allocatorIiE8allocateEmPKv[__gnu_cxx::new_allocator<int>::allocate(unsigned long, void const*)]+0x45): undefined reference tooperator new(unsigned long)'
/tmp/ccV3VLsK.o:(.eh_frame+0x12): 未定义引用 __gxx_personality_v0' <br/> /tmp/ccV3VLsK.o:(.eh_frame+0x4f): undefined reference to__gxx_personality_v0'
collect2: ld 返回 1 个退出状态

【问题讨论】:

  • min_element(choices.begin(),choices.end());
  • 小心using namespace std;。您有一个名为min 的变量和一个名为min 的函数。这不是一场冲突,但已经接近了。
  • min_element 工作正常。你只是没有正确使用它。您可能想更改问题标题。
  • 是的,我更改了它,按照您的建议使用它,但仍然无法正常工作。

标签: c++ linux redhat min minimum


【解决方案1】:

std::min_element 取一个范围。 frontback 返回对实际值的引用。您应该使用 beginend 方法将迭代器返回到向量中的相应位置:

min_element(choices.begin(), choices.end());
//                  ^^^^^            ^^^

如果您觉得这很乏味,您可以创建一个环绕标准 min_element 的函数:

template <class Container>
auto min_element(Container c) -> decltype(std::min_element(c.begin(), c.end()))
{
    return std::min_element(c.begin(), c.end());
}

并将其用作:

min_element(choices);

【讨论】:

  • 我已将其更改为您所说的,并得到以下错误:burningcoins.cpp:在函数'int checkchange(int)'中:burningcoins.cpp:19:错误:无法转换'__gnu_cxx: :__normal_iterator > >' 作为回报我想那是因为它返回一个指针。但是使用星号运算符取消引用它会给我带来更多错误?
  • @BananaCode 这是你所做的唯一改变吗?
  • 是的,这是唯一的变化。
  • @BananaCode 取消引用时会出现哪些错误。在这种情况下,取消引用是正确的做法。
  • burningcoins.cpp:在函数'int checkchange(int)'中:burningcoins.cpp:20:错误:';'令牌之前的预期主表达式。这是代码的相关部分:return min_element(choices.begin(),choices.end())*;
猜你喜欢
  • 1970-01-01
  • 2014-05-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-20
  • 1970-01-01
  • 2012-04-22
相关资源
最近更新 更多