从第一个元素的memoryadress中记忆myvect.size()*sizeof(foo)字节是否安全?
std::vector<std::pair<T1, T2> > myvect
Run Code Online (Sandbox Code Playgroud)
成阵列
struct foo{
T1 first;
T2 second;
}
Run Code Online (Sandbox Code Playgroud)
如果为数组分配的元素数与向量的大小相同?
谢谢
我挖了一个旧项目并想编译它,但收到了几个错误,其中一些是c2664:
error C2664: 'std::make_pair' : cannot convert parameter 1 from 'CUser *' to 'CUser *&&'
error C2664: 'std::make_pair' : cannot convert parameter 1 from 'unsigned long' to ' unsigned long &&'
Run Code Online (Sandbox Code Playgroud)
相关的代码部分是:
//typedef for the userdata map
typedef std::map<unsigned long, std::pair<CUser*,userstatus*>> UserDataMapType;
//...
Inc::incret CUserManager::AddUser(unsigned long ID, CUser* pUser, userstatus* pUserStatus)
{
//...
std::pair<UserDataMapType::iterator, bool> ret = m_mapUserData.insert(std::make_pair<unsigned long, std::pair<CUser*, userstatus*>>(ID, std::make_pair<CUser*, userstatus*>(pUser, pUserStatus)));
//...
}
Run Code Online (Sandbox Code Playgroud)
我试图制作函数参数const,但这没有帮助.
它确实在VS2010中编译得很好.
请帮我找到导致此问题的原因以及解决方法.
在尝试编译一个简单的类(g++ myclass.cpp)时,我收到以下错误:
#ifndef MYCLASS
#define MYCLASS
#include <iostream>
#include <tuple>
class MyClass {
std::tuple<bool, int, int> my_method();
};
#endif
Run Code Online (Sandbox Code Playgroud)
我搜索了这个问题,在大多数情况下,人们似乎忘了std::或包括<tuple>在标题中.但我有两个.这是我的代码:
myclass.h
#include "myclass.h"
using namespace std;
tuple<bool, int, int> MyClass::my_method() {
return make_tuple(true, 1, 1);
}
Run Code Online (Sandbox Code Playgroud)
myclass.cpp
#ifndef MYCLASS
#define MYCLASS
#include <iostream>
#include <tuple>
class MyClass {
std::tuple<bool, int, int> my_method();
};
#endif
Run Code Online (Sandbox Code Playgroud)
如果我使用pair相反的方法,省略第二个int和包括<set>,它的工作原理.
我错过了什么?
编辑:
这是完整的输出:
#include "myclass.h"
using namespace std;
tuple<bool, int, int> MyClass::my_method() { …Run Code Online (Sandbox Code Playgroud) 我不清楚为什么分配是合法的 tuple<X,Y>=pair<X,Y>
但是分配是违法的 pair<X,Y>=tuple<X,Y>
std::pair<int, double> x { 1 , 5.5};
std::tuple<int, double> y { 1 , 5.5};
int a;
double b;
std::tie(a,b) = x;
std::tie(a,b) = y;
x = y; // THIS LINE (line 12)
y = x; // but this is fine ???
Run Code Online (Sandbox Code Playgroud)
这不应该是对称的吗?
使用g ++ 4.8.1会出现以下错误:
tp.cpp:12:4: error: no match for operator= (operand types are std::pair<int, double> and std::tuple<int, double>)
x = y;
^
tp.cpp:12:4: note: candidates are:
In file included from /opt/gcc-4.8.1/include/c++/4.8.1/utility:70:0,
from /opt/gcc-4.8.1/include/c++/4.8.1/tuple:38,
from tp.cpp:1: …Run Code Online (Sandbox Code Playgroud) 我尝试构造整数对,其中第二个整数大于第一个整数1:
1 2
2 3
3 4
Run Code Online (Sandbox Code Playgroud)
std::make_pair像这样使用两者和构造函数:
std::make_pair(n, n++);
Run Code Online (Sandbox Code Playgroud)
但是,这会导致对相反:
2 1
3 2
4 3
Run Code Online (Sandbox Code Playgroud)
如果我将后增量放在第一个参数上或(n+1)改为使用,我会得到所需的结果。
为什么会这样?
我有一个std::list< std::pair<std::string,double> >,我知道按照分类排序std::string element.
因为我希望做了很多std::find_if基于对std::string元素,相信std::map<string,double,MyOwnBinaryPredicate>与lower_bound和upper_bound会更充足.
事实是,我希望以有效的方式使用insert元素std::map.所以我想使用额外的迭代器来insert加快速度.
我认为,最简单的方法是使用一个const_reverse_iterator要经过std::list和使用begin()的std::map.
你会这样做,还是一个坏主意?
谢谢!
我正在使用g ++ 4.4.7进行编译(当前不能更高),并使用-std=gnu++0x编译器开关,它应该允许第三行的语法.
typedef std::vector<CI_RecordInfo_Pair> CI_RecordInfo_Vector;
typedef std::vector<std::pair<std::string, CI_RecordInfo_Vector*> > MgrBlks;
MgrBlks mgr_n_blks { {"T2M_NAME", NULL} }; // <--- line 59
Run Code Online (Sandbox Code Playgroud)
但是,编译器抱怨如下:
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/stl_pair.h: In constructor 'std::pair<_T1, _T2>::pair(_U1&&, _U2&&) [with _U1 = const char (&)[9], _U2 = long int, _T1 = std::basic_string<char, std::char_traits<char>, std::allocator<char> >, _T2 = CI_RecordInfo_Vector*]':
tom.cpp:59: instantiated from here
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/stl_pair.h:90: error: invalid conversion from 'long int' to 'CI_RecordInfo_Vector*'
Run Code Online (Sandbox Code Playgroud)
我假设"long int"是NULL,并且由于某种原因我无法将其转换为指针.然而在结构图的其他地方,我能够编译类似的东西
foo["X"] = { NULL, "bar", 12 }; // first element is a pointer
Run Code Online (Sandbox Code Playgroud)
有什么不同?
为什么我不能从一对中返回unique_ptr?
#include <iostream>
#include <memory>
#include <utility>
using namespace std;
unique_ptr<int> get_value() {
pair<unique_ptr<int>, int> p(unique_ptr<int>(new int(3)), 4);
return p.first;
}
int main(void) {
cout << *get_value() << endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
当我尝试用g ++ 4.6编译它时,我得到:
../main.cpp: In function ‘std::unique_ptr<int> get_value()’:
../main.cpp:9:11: error: use of deleted function ‘std::unique_ptr<_Tp, _Dp>::unique_ptr(const std::unique_ptr<_Tp, _Dp>&) [with _Tp = int, _Dp = std::default_delete<int>, std::unique_ptr<_Tp, _Dp> = std::unique_ptr<int>]’
/usr/include/c++/4.6/bits/unique_ptr.h:256:7: error: declared here
make: *** [main.o] Error 1
Run Code Online (Sandbox Code Playgroud)
我不明白错误信息
考虑以下代码:
#include <utility>
#include <vector>
using V = std::vector<int>;
int main() {
std::pair<int, V> p1{1, 2}; // p1.second has 2 elements
std::pair<int, V> p2{1, {2}}; // p2.second has 1 element
std::pair<V, V> p3{2, 2}; // Both vectors have 2 elements
std::pair<V, V> p4{{2}, {2}}; // Both vectors have 1 element
std::pair<V, V> p5{2, {2}}; // Does not compile
// p5.first should have 2 elements, while the other should have 1
}
Run Code Online (Sandbox Code Playgroud)
我的主要问题是最后一行 ,p5它不能编译,g++-12但可以编译g++-10。我想知道:
为什么下面的C++代码无法编译?
#include <utility>
int main() {
int x[6];
int y[6];
std::pair<int[6], int[6]> a(x, y);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
例如 MSVC 给出以下错误:
错误 C2661: 'std::pair<int [6],int [6]>::pair': 没有重载函数需要 2 个参数
感谢使用C++替代品构造数组的评论,但我也想知道代码无法编译的原因。