c++ - .begin() 和 .end() 上未解析的标识符?

标签 c++ vector identifier

我在 l_vector.begin()l_vector.end 上不断收到“ Unresolved 识别”错误,特别是在 beginend 函数,为什么它不能识别这些简单的 vector 函数?

#include <cstdlib>
#include <iostream>
#include <ctime>
#include <vector>
#include <string>

using namespace std;
// function prototype
int FindIndexOfLargest(vector<int> l_vector);

int main() 
{
    int FindIndexOfLargest();
    return 0;
}

//function definition
int FindIndexOfLargest(vector<int> l_vector)
{

     vector<int>:: const_iterator iter;
     int current_max_location = 0;
     int current_max = 0;


    for(iter = l_vector.begin(); iter != l_vector.end(); ++iter)
    {
        if(*iter > current_max)
        {
            current_max_location = iter;
        }
        return current_max;
    }
}

最佳答案

我看到编译失败的几个原因。

int FindIndexOfLargest();

你必须传递一个 std::vector <int>main() 中调用此函数时.

最后,你的函数 int FindIndexOfLargest(vector<int> l_vector)必须返回 int值(value)。

编写此函数的另一种方法是使用 std::max_elementstd::distance .

std::size_t FindIndexOfLargest(const std::vector <int> &input)
{
    std::vector<int>::const_iterator max_value = 
        std::max_element(input.cbegin(), input.cend());

    if (max_value != input.cend()) {
        return std::distance (input.cbegin(), max_value);
    }

    // Otherwise, throw an expection or return an error value here.
}

关于c++ - .begin() 和 .end() 上未解析的标识符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36228032/

相关文章:

ios - 为现有 AppStore 应用重命名 Xcode 项目

C++:如果我不使用 move ,我还需要关心复制控制吗?

c++ - vector<vector<int>> 初始化时间太长

c++ - DEFINE 宏是否适用于所有平台?

qt - QT中的QVector数组无法访问私有(private)成员错误

PostgreSQL 错误 : Relation already exists

c++ - 使用 Docker 构建已编译的应用程序

java - 通用简单 3D 矩阵旋转问题

c++ - 如果我在指向 vector 空元素的迭代器上调用方法,会发生什么?

java - Switch 语句 - 类型的非法开始、预期的标识符和孤立的大小写?