代码之家  ›  专栏  ›  技术社区  ›  spurra

无法使min_element在C中工作++

  •  0
  • spurra  · 技术社区  · 11 年前

    我正在写一个小的递归算法。这是代码:

    #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/算法:62,
    来自burningcoin.cpp:3:/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../include/c++/4.4.7/bits/sstl_algo.h:
    在函数_FIter std::min_element(_FIter,_FIter)[带_FIter=
    int]:burningcoins.cpp:19:从这里实例化
    /usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../include/c++/4.4.7/bits/str_algo.h:5998:
    错误:一元的类型参数无效*
    /usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../include/c++/4.4.7/bits/str_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 to 标准::cin’
    burningcoins.cpp:(.text+0x16f):未定义对的引用 std::basic_istream<char, std::char_traits<char> >::operator>>(int&)' <br/> burningcoins.cpp:(.text+0x187): undefined reference to 标准::cin’
    burningcoins.cpp:(.text+0x18c):未定义对的引用 std::basic_istream<char, std::char_traits<char> >::operator>>(int&)' <br/> burningcoins.cpp:(.text+0x19b): undefined reference to 标准::基本流>:运算符>>(int和amp;)'
    burningcoins.cpp:(.text+0x10b0):未定义对的引用 std::cin' <br/> burningcoins.cpp:(.text+0x1b5): undefined reference to 标准::基本流>:运算符>>(int和amp;)'
    burningcoins.cpp:(.text+0x1ec):未定义对的引用 std::cin' <br/> burningcoins.cpp:(.text+0x1f1): undefined reference to 标准::基本流>:运算符>>(int和amp;)'
    burningcoins.cpp:(.text+0x208):未定义对的引用 std::cout' <br/> burningcoins.cpp:(.text+0x20d): undefined reference to 标准::基本流>:运算符<&书信电报;(int)'
    /tmp/ccV3VLsK.o:在功能中 __static_initialization_and_destruction_0(int, int)': <br/> burningcoins.cpp:(.text+0x261): undefined reference to std::ios_base::Init::Init()'
    burningcoins.cpp:(.text+0x266):未定义对的引用 std::ios_base::Init::~Init()' <br/> /tmp/ccV3VLsK.o: In function 标准::矢量>:_M_range_check(无符号长)常量':
    burningcoins.cpp:(.text._ZNKSt6vectorIiSaIIE14_M_range_checkEm[std::vector>:_M_range_check(unsigned long)const]+0x2d):未定义的引用 std::__throw_out_of_range(char const*)' <br/> /tmp/ccV3VLsK.o: In function 标准::矢量>:_M_insert_aux(__gnu_cx::__normal_iterator>>,int常量&)':
    burningcoins.cpp:(.text._ZNSt6vectorIiSaIiEE13_M_insert_auxEN9__gnu_xx17__normal_iteratorPiS1_EERKi[std::vector>:_M_insert_aux(__gnu_cx::__normal_isterator>,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(重试)'
    burningcoins.cpp:(.text._ZNSt6vectorIiSaIiEE13_M_insert_auxEN9__gnu_xx17__normal_iteratorPiS1_EERKi[std::vector>:_M_insert_aux(__gnu_cx::__normal_isterator>,int const&)]+0x2c8):未定义的引用 __cxa_end_catch' <br/> /tmp/ccV3VLsK.o: In function 标准::矢量>:_M_check_len(无符号长字符,字符常量*)常量':
    burningcoins.cpp:(.text._ZNKSt6vectorIiSaIIE12_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::解除分配(int*,无符号长)':
    burningcoins.cpp:(.text._ZN9__gnu_cxx13new_alloctorIie10deallocateEPim[__gnu_xx::new_allocator::deallocate(int*,unsigned long)]+0x1c):对的未定义引用 operator delete(void*)' <br/> /tmp/ccV3VLsK.o: In function __gnu_cxx::new_allocater::allocate(无符号长,无效常量*)':
    burningcoins.cpp:(.text._ZN9__gnu_cxx13new_alloctorIie8allocateEmPKv[__gnu_xx::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 to 运算符new(无符号长)'
    /tmp/ccV3VLsK.o:(.eh_frame+0x12):未定义对的引用 __gxx_personality_v0' <br/> /tmp/ccV3VLsK.o:(.eh_frame+0x4f): undefined reference to __gxx_个人素质_v0'
    collect2:ld返回1退出状态

    1 回复  |  直到 8 年前
        1
  •  4
  •   David G    11 年前

    std::min_element 需要一个范围。 front back 返回对实际值的引用。您应该使用 begin end 方法将迭代器返回到向量中的相应位置:

    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);