对向量 c++ 中的上限
#include <algorithm>
#include <iostream>
#include <iterator>
#include <string>
#include <vector>
#include <utility>
using namespace std;
typedef pair<int,int> Pair;
inline bool less_than_second( const Pair& b1, const Pair& b2 ){
return b1.second < b2.second;
}
int main()
{
const int SP1[] = { 2,53,21,55,36,5,1};
const int EP1[] = { 18, 20, 26, 30, 41,1,5 };
int i;
const int num_pairs = sizeof( SP1 ) / sizeof( SP1[0] );
vector<int> sm(num_pairs);
// vector <int> SP;
vector<Pair> pair( num_pairs );
transform( EP1, EP1+num_pairs, SP1,pair.begin(), make_pair<int,int> );// MAKE PAIR
sort( pair.begin(), pair.end() );
sort( pair.begin(), pair.end(), less_than_second );
vector<Pair>::const_iterator pair_end = pair.end();
vector<int> SP,EP;
vector<int>::iterator low,up;
for( vector<Pair>::const_iterator ptr = pair.begin();ptr != pair_end; ++ptr )
{
int SP = ptr->second;
int EP = ptr->first;
cout<<"("<<SP<<","<<EP<<")\n";
}
//cout<<"("<<SP<<","<<EP<<")\n";
low=lower_bound (SP.begin(), SP.end(), 20);
up= upper_bound (SP.begin(), SP.end(), 20);
cout << "lower_bound at position " << int(low- SP.begin()) << endl;
cout << "upper_bound at position " << int(up - SP.begin()) << endl;
up= upper_bound (pair.begin(), pair.end(), 20);
cout << "upper_bound at position " << int(up - pair.begin()) << endl;
getchar();
}
我对向量对进行了排序,我试图获取向量对中一个向量的 upper_bound 值,但它给了我 upper_bound 位于位置 = 0。
请耐心等待,我是 c++ 的新手,想学习。请帮助修复此代码。谢谢
#include <algorithm>
#include <iostream>
#include <iterator>
#include <string>
#include <vector>
#include <utility>
using namespace std;
typedef pair<int,int> Pair;
inline bool less_than_second( const Pair& b1, const Pair& b2 ){
return b1.second < b2.second;
}
int main()
{
const int SP1[] = { 2,53,21,55,36,5,1};
const int EP1[] = { 18, 20, 26, 30, 41,1,5 };
int i;
const int num_pairs = sizeof( SP1 ) / sizeof( SP1[0] );
vector<int> sm(num_pairs);
// vector <int> SP;
vector<Pair> pair( num_pairs );
transform( EP1, EP1+num_pairs, SP1,pair.begin(), make_pair<int,int> );// MAKE PAIR
sort( pair.begin(), pair.end() );
sort( pair.begin(), pair.end(), less_than_second );
vector<Pair>::const_iterator pair_end = pair.end();
vector<int> SP,EP;
vector<int>::iterator low,up;
for( vector<Pair>::const_iterator ptr = pair.begin();ptr != pair_end; ++ptr )
{
int SP = ptr->second;
int EP = ptr->first;
cout<<"("<<SP<<","<<EP<<")\n";
}
//cout<<"("<<SP<<","<<EP<<")\n";
low=lower_bound (SP.begin(), SP.end(), 20);
up= upper_bound (SP.begin(), SP.end(), 20);
cout << "lower_bound at position " << int(low- SP.begin()) << endl;
cout << "upper_bound at position " << int(up - SP.begin()) << endl;
up= upper_bound (pair.begin(), pair.end(), 20);
cout << "upper_bound at position " << int(up - pair.begin()) << endl;
getchar();
}
I Sorted the pair vector and I am trying to get the values of upper_bound of one the vector in the pair, but it gives me
upper_bound at position = 0.
Please bear with me, I am a newbie to c++ and want to learn. Please help to fix this code. Thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
据我所知,您从未将任何数据放入
SP
向量中。您的意思可能不是int SP = ptr->second;
,而是SP.push_back(ptr->second);
。附带说明一下,由于排序不稳定,因此在使用谓词对其进行排序之前调用
sort(pair.begin(),pair.end() );
是没有意义的。最后,您可能希望在 《权威 C++ 图书指南》中挑选一本书和列表来帮助您学习语言。
As far as I can tell, you never put any data into the
SP
vector. It's possible that instead ofint SP = ptr->second;
you meantSP.push_back(ptr->second);
.As a side note, since sort isn't stable there's no point in calling
sort( pair.begin(), pair.end() );
before you sort it with your predicate.Finally, you may wish to pick up one of the books in The Definitive C++ Book Guide and List to help you learn the language.