可以将lower_bound(来自c++ STL)的结果赋值给整数类型变量吗?
Can the result of lower_bound(From C++ STL) be assigned to an integer type variable?
我最近开始使用c++ STL,今天我在我的代码中试用了lower_bound
函数。但不幸的是,我得到了错误:
cannot convert '__gnu_cxx::__normal_iterator<long long int*, std::vector<long long int> >' to 'long long int' in assignment|
我的代码
lli n;
cin >> n;
lli k;
cin >> k;
vector<lli> v;
lli store;
for(lli i = 0;i < n;i++)
{
cin >> store;
if(store < k)
v.push_back(store);
}
sort(v.begin(),v.end());
lli paths = 0;
for(lli i = (lli)v.size()-1;i >= 0;i--)
paths = paths + lower_bound(v.begin(),v.begin()+i-1,v[i]-k);
cout << paths;
如果你说迭代器类型变量不能添加到整数类型变量中,下面的代码:
https://github.com/sampritipanda/IOI_Repository/blob/master/ZCO/2013/CHEWING.cpp使用lower_bound的结果赋值给整型变量,通过以下行:
int j = lower_bound(gum.begin() + i, gum.end(), K - gum[i]) - (gum.begin() + i);
请告诉我哪里错了。
注意:lli
代表已经用宏定义的long long int
。
不能将迭代器赋值给整型变量。
lower_bound(gum.begin() + i, gum.end(), K - gum[i]) - (gum.begin() + i);
将返回std::vector<int>::difference_type
对于你的情况,你能做的就是:
paths = paths + (lower_bound(v.begin(),v.begin()+i-1,v[i]-k) - v.begin());
相关文章:
- 为"adjacent"变量赋值时出现问题
- C++中的赋值发生,尽管右侧出现异常
- 用C++中的sscanf赋值
- 为std::string的某个索引赋值
- 重载Singly Linked List中的赋值运算符
- 为什么我必须在C++中添加一个赋值符号来声明一个数组
- gtest_使用setargpointee在函数中赋值
- 非常量变量只读位置的赋值
- 使用赋值运算符重载从类中返回jobject
- C++数据文件、数组和计算赋值
- 对条件表达式结果的赋值(其中第二个和第三个操作数是相同类型和值类别的变量)是否仍然存在?
- 赋值逻辑操作的结果
- 溢出的有符号/无符号赋值及其结果
- 可以将lower_bound(来自c++ STL)的结果赋值给整数类型变量吗?
- 使用复合赋值的结果作为左值
- 在本例中,赋值运算符重载解析是如何工作的?结果出乎我意料
- 我可以将赋值的结果传递给c++中的函数吗?
- 将一个char指针赋值给另一个(不同的地址)的意外结果
- 在程序中使用非类型模板参数而未给局部变量赋值时,会导致意外结果
- 操作未赋值给变量的结果