假设我有以下代码:
std::vector< std::pair <int, char> > myVec;
or
std::list< std::pair <int, char> > myList;
/* then ***************/
std::list< std::pair <int, char> >::iterator listIt;
or
std::vector< std::pair <int, char> >::iterator vectorIt;
/* No difference between vector and list */
Run Code Online (Sandbox Code Playgroud)
现在我需要搜索其中的一个int元素,所以:
vectorIt = std::find_if(myVec.begin(),myVect.end(),make_pair(.....));
^^^^^^^^^^^^^^^^^
Run Code Online (Sandbox Code Playgroud)
我该怎么做?
我注意到std :: pair在尝试将其保存到二进制文件时发生了一件令人不愉快的事情:std :: pair与一个单词对齐.它在处理器效率方面可能很有用,但需要更多的存储空间,所以我想将对齐模式切换为1字节的std :: pair.我的编译器是MS VC++ 2012.
#include <iostream>
int main( )
{
struct S_a { double a; size_t b; };
#pragma pack(1)
struct S_wa { double a; size_t b; };
std::cout << sizeof( size_t ) << '\n'; // 4
std::cout << sizeof( double ) << '\n'; // 8
std::cout << sizeof( std::pair< size_t, size_t > ) << '\n'; // 8
std::cout << sizeof( std::pair< double, size_t > ) << '\n'; // 16 - bad
std::cout << sizeof( …Run Code Online (Sandbox Code Playgroud) 我正在尝试进行包含整数对向量和整数的binary_search,如下所示:
#include <vector>
#include <algorithm>
using namespace std;
typedef vector<pair<size_t,size_t> > int_pairs;
bool operator<(const size_t& l, const pair<size_t,size_t>& r)
{return r.first < l;} // useful for binary search
int main(){
int_pairs pairs_vec;
pairs_vec.push_back(pair <size_t,size_t>(1,2));
pairs_vec.push_back(pair <size_t,size_t>(2,2));
size_t i(2);
binary_search(pairs_vec.begin(),pairs_vec.end(),i);
}
Run Code Online (Sandbox Code Playgroud)
编译器告诉我operator<没有定义:
erreur: no match for ‘operator<’ (operand types are ‘const long unsigned int’ and ‘std::pair<long unsigned int, long unsigned int>’)
Run Code Online (Sandbox Code Playgroud)
我是以正确的方式做到的吗?我试图以许多不同的方式改变运算符的定义,但似乎没有任何效果.
我不知道如何才能在C++中完成这项工作.
意图是:
pair<int, int> foo() {
if(cond) {
return std::make_pair(1,2);
}
return NULL; //error: no viable conversion from 'long' to 'pair<int, int>
}
void boo() {
pair<int, int> p = foo();
if (p == NULL) { //error: comparison between NULL and non-pointer ('int, int' and NULL)
// doA
} else {
int a = p.first;
int b = p.second;
// doB
}
}
Run Code Online (Sandbox Code Playgroud)
由于我不能在C++中使用return NULL,这是我的第二次尝试:
pair<int, int>* foo() {
if(cond) {
return &std::make_pair(1,2); //error: returning address of local temporary object) …Run Code Online (Sandbox Code Playgroud) 当我在地图中使用对作为关键时,我试图获得对的第一和第二元素.为了更好地澄清,请参阅下面的代码.这是我试过的
#include <bits/stdc++.h>
using namespace std;
int main()
{
// your code goes here
map<pair<int,int>,int>mp;
int n;
cin>>n;
int a[n];
for(int i=0;i<n;++i)cin>>a[i];
int y=0;
for(int i=0;i<n;++i)
{
mp.insert(make_pair(y,a[i]));
y=a[i]+1;
}
int m;
cin>>m;
int q[m];
for(int i=0;i<m;++i)cin>>q[i];
for(int i=0;i<m;i++)
{
int temp=q[i];
for(map<pair<int,int>,int>::iterator it=mp.begin();it!=mp.end();++it)
{
if(((it->first)<=temp)&&((it->second)>=temp))
cout<<mp->second<<endl;
}
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我想在这里获得关键的第一和第二要素.我怎么能这样做?
请考虑以下代码段,其中第一行仅用作前向声明
class A;
Run Code Online (Sandbox Code Playgroud)
然后定义新类
class B
{
vector<A> Av; //line 1
map<int, A> Am; //line 2
pair<int, A> Ap; //line 3
};
Run Code Online (Sandbox Code Playgroud)
第1行和第2行似乎没有前向声明(这可能告诉我那些容器使用指针类型的实现),其中第3行似乎不在VS2012上编译.
我的问题是标准或特定于我正在使用的编译器所规定的行为?
谢谢
我不知道如何创建以下内容:
std::pair<std::atomic<bool>, int>
Run Code Online (Sandbox Code Playgroud)
我总是总是得到
/usr/include/c++/5.5.0/bits/stl_pair.h:139:45:错误:使用已删除的功能'std :: atomic :: atomic(const std :: atomic&)'
:第一(__x),第二(std :: forward <_U2>(__ y)){}
我试过了
std::pair<std::atomic<bool>, int> pair = std::make_pair(true, 1); //doesn't work
std::pair<std::atomic<bool>, int> pair = std::make_pair({true}, 1); //doesn't work
std::pair<std::atomic<bool>, int> pair = std::make_pair(std::atomic<bool>(true), 1); //doesn't work
std::pair<std::atomic<bool>, int> pair = std::make_pair(std::move(std::atomic<bool>(true)), 1); //doesn't work
Run Code Online (Sandbox Code Playgroud)
我知道std :: atomic是不可复制的,那么您应该如何成对创建它?只是不可能吗?
根据cppreference:
在不等式比较(<,>)中,首先比较第一个元素,并且只有当不等式比较不适用于它们时,才比较第二个元素.
这意味着:
return ((a.first < b.first) || (!(b.first < a.first) && (a.second < b.second)));
Run Code Online (Sandbox Code Playgroud)
我的问题是,为什么它如此不直观?它背后的原因是什么?是否存在这种推理导致正确答案的例子?
我认为实施将只是:
return a.first < b.first && a.second < b.second
Run Code Online (Sandbox Code Playgroud) 有一个非常流行的问题是"std :: pair vs struct with two fields".但我有一个关于重新分配first和second值到语义命名变量的问题.在常规情况下,我们有这样的事情:
const std::pair<const int, const int> result = processSomething();
std::cout << result.second << " of " << result.first << std::endl;
Run Code Online (Sandbox Code Playgroud)
但是如果我们首先将它们分配给引用变量呢:
const std::pair<const int, const int> result = processSomething();
const int &numTotal = result.first;
const int &numSuccessful = result.second;
std::cout << numSuccessful << " of " << numTotal << std::endl;
Run Code Online (Sandbox Code Playgroud)
这使我们免于撰写关于first和的语义的评论second.这种方式有什么缺点?编译器会为numTotal和保留堆栈numSuccessful吗?如果在主循环应用中使用此模式,性能是否会下降?
从常规变量更改为引用变量(感谢您的评论)
我知道我们需要包括一些比较功能才能实现此目的。
但无法为此写。
例如:
向量的元素={(2,4),(4,2),(5,1),(5,3)}
找到= 5
lower_bound()应该返回2
代码->
#define pp pair<int,int>
bool cmp(const pp &l,const pp &r) {
return l.first < r.first;
}
int main() {
vector<pp> v;
sort(v.begin(), v.end(), cmp);
int id=(int)(lower_bound(v.begin(), v.end(), ??) - v.begin());
}
Run Code Online (Sandbox Code Playgroud)