在C++中有没有针对long长数据类型的 lower_bound()函数?

问题描述 投票:0回答:1

pos 是否有一个迭代器,可以求出 q. 喏 q 是一个很长很长的整数,并且 prefix 是一个存储长长元素的向量。

vector <int> :: iterator pos; 
pos = lower_bound(prefix.begin(), prefix.end(), q);

我得到以下错误。

no operator "=" matches these operands -- operand types are: __gnu_cxx::__normal_iterator<int *, std::vector<int, std::allocator<int>>> = __gnu_cxx::__normal_iterator<long long *, std::vector<long long, std::allocator<long long>>>

有什么方法可以解决这个问题吗?

c++ vector binary-search lower-bound
1个回答
3
投票

你对 pos 是不正确的。你的矢量 - prefix 是的宣布为 vector<long long>prefix;. 迭代器也应该是相同的类型。

你可以尝试使用以下方法

vector<long long>::iterator pos = lower_bound(prefix.begin(), prefix.end(), q);

如果你不确定如何正确地声明变量(本例中的迭代器),你也可以选择以下语法。

auto pos = lower_bound(prefix.begin(), prefix.end(), q);

在这里,你可以选择以下语法 auto 关键字只能在C++11及以上版本中使用。但是,如果你确定你要声明的变量的语法和类型,那么总是 最好是手动申报 而不是由你的程序来推断。它将 提高可靠性和可读性 的程序。

希望这能解决您的问题

© www.soinside.com 2019 - 2024. All rights reserved.