有什么区别:
std::shared_ptr<int> p = std::shared_ptr<int>( new int );
Run Code Online (Sandbox Code Playgroud)
和
std::shared_ptr<int> p = std::make_shared< int >();
Run Code Online (Sandbox Code Playgroud)
?
我应该选择哪一个?为什么?
PS相当肯定这已经得到了答案,但我找不到类似的问题.
所有,
这个问题的延续这一个.我认为STL错过了这个功能,但它只是我的恕我直言.
现在,问题.
考虑以下代码:
class Foo
{
public:
Foo();
int paramA, paramB;
std::string name;
};
struct Sorter
{
bool operator()(const Foo &foo1, const Foo &foo2) const
{
switch( paramSorter )
{
case 1:
return foo1.paramA < foo2.paramA;
case 2:
return foo1.paramB < foo2.paramB;
default:
return foo1.name < foo2.name;
}
}
int paramSorter;
};
int main()
{
std::vector<Foo> foo;
Sorter sorter;
sorter.paramSorter = 0;
// fill the vector
std::sort( foo.begin(), foo.end(), sorter );
}
Run Code Online (Sandbox Code Playgroud)
在任何给定的时刻,矢量都可以重新排序.该类还具有在分类器结构中使用的getter方法.
在向量中插入新元素的最有效方法是什么?
我的情况是:
我有一个网格(电子表格),它使用类的排序向量.在任何给定时间,可以重新排序向量,并且网格将相应地显示排序的数据.
现在我需要在向量/网格中插入一个新元素.我可以插入,然后重新排序然后重新显示整个网格,但这对于大网格来说效率非常低. …
我有一个C项目在Linux中编译和运行.这是一个包含许多子目录的非常大的项目.在父目录中有文件Makefile.am
和Makefile.in
.
我试过运行make -f Makefile.am
,并得到以下错误:
make: Nothing to be done for `Makefile.am'.
Run Code Online (Sandbox Code Playgroud)
这是什么意思?我如何完成任务?
这是我的代码示例:
class X
{
public:
void f() {}
};
class Y : public X
{
public:
X& operator->() { return *this; }
void f() {}
};
int main()
{
Y t;
t.operator->().f(); // OK
t->f(); // error C2819: type 'X' does not have an overloaded member 'operator ->'
// error C2232: '->Y::f' : left operand has 'class' type, use '.'
}
Run Code Online (Sandbox Code Playgroud)
为什么编译器试图将operator->的责任从Y移到X?当我实现X :: op->然后我不能返回X那里 - 编译错误说"无限递归",而从X :: op->返回一些Z再次说Z没有operator->,因此更高和等级越高.
谁能解释这个有趣的行为?:)
所以在 C++ 中,现在 make_from_tuple 为:
T obj = std::make_from_tuple<T>( { Args... args } ); // args represents a tuple
Run Code Online (Sandbox Code Playgroud)
但人们会怎么做:
T* obj = std::make_new_from_tuple<T*>( { Args... args } );
Run Code Online (Sandbox Code Playgroud)
有 make_shared 和 make_unique 但它们都不接受元组(而且我不确定如何从元组中提取参数,如果这是要走的方向,因为如果您想要原始指针,您总是可以 make_unique 然后释放) 。
非常简单的例子1:
struct A
{
int i_; double d_; std::string s_;
A( int i, double d, const std::string& s ) : i_(i), d_(d), s_(s) {}
};
auto aTuple = std::make_tuple( 1, 1.5, std::string("Hello") );
Run Code Online (Sandbox Code Playgroud)
对于更复杂的示例,如果元组包含要转发的 unique_ptr,我也希望它能够工作。
为什么我可以在免费存储上创建具有私有析构函数的类的对象但不在堆栈上?
例如,这是非法的:
class Foo
{
public:
explicit Foo( int );
static void delete_foo(Foo* foo ) { delete foo; }
private:
int x;
~Foo();
Foo( const Foo& );
Foo& operator=(const Foo& );
};
int main()
{
Foo * fooptr = new Foo(5); // legal
Foo::delete_foo( fooptr ); // legal
Foo foo(5); // illegal
}
Run Code Online (Sandbox Code Playgroud) 假设我有两个.cpp文件,file1.cpp和file2.cpp,它们都使用std::vector<int>
.假设file1.cpp有一个int main(void)
.如果我将两者编译成file1.o和file2.o,并将两个目标文件链接到我可以执行的elf二进制文件中.我正在编译32位Ubuntu Linux机器.
我的问题是关于编译器和链接器如何将std :: vector的符号放在一起:
std::vector
的std::vector
代码和另一组代码用于包含f2.o的代码?我为自己尝试了这个(我用过g++ -g
),然后我查看了我的最终可执行文件反汇编,我发现为向量构造函数和其他方法生成的标签显然是随机的,尽管来自f1.o的代码似乎调用了相同的构造函数.来自f2.o的代码.但是,我无法确定.
如果链接器确实阻止了代码重复,它是如何做到的?它必须"知道"什么模板?它是否始终阻止关于跨多个目标文件多次使用相同模板化代码的代码重复?
这是一个说明问题的最小代码示例:
#include <iostream>
class Thing
{
// Non-copyable
Thing(const Thing&);
Thing& operator=(const Thing&);
int n_;
public:
Thing(int n) : n_(n) {}
int getValue() const { return n_;}
};
void show(const Thing& t)
{
std::cout << t.getValue() << std::endl;
}
int main()
{
show(3);
}
Run Code Online (Sandbox Code Playgroud)
这会产生相同的错误:
int main()
{
show( Thing(3) );
}
Run Code Online (Sandbox Code Playgroud)
AIX下的IBM XL C/C++ 8.0编译器会发出以下警告:
"testWarning.cpp", line 24.9: 1540-0306 (W) The "private" copy constructor "Thing(const Thing &)" cannot be accessed.
"testWarning.cpp", line 24.9: 1540-0308 (I) The semantics specify that …
Run Code Online (Sandbox Code Playgroud) 以下是memcmp的Microsoft CRT实现:
int memcmp(const void* buf1,
const void* buf2,
size_t count)
{
if(!count)
return(0);
while(--count && *(char*)buf1 == *(char*)buf2 ) {
buf1 = (char*)buf1 + 1;
buf2 = (char*)buf2 + 1;
}
return(*((unsigned char*)buf1) - *((unsigned char*)buf2));
}
Run Code Online (Sandbox Code Playgroud)
它基本上执行逐字节比较.
我的问题分为两部分:
count < sizeof(int)
,然后逐字节比较为剩下的内容做什么?注意:我根本不使用CRT,所以无论如何我必须实现这个功能.我只是在寻找有关如何正确实现它的建议.
我有另一个问题,我似乎无法解决......,或在这个网站上找到...
我有一个带有地图的对象(称为DataObject),声明如下:
std::map<size_t, DataElement*> dataElements;
Run Code Online (Sandbox Code Playgroud)
现在我有一个复制功能(在复制构造函数中使用):
void DataObject::copy(DataObject const &other) {
//here some code to clean up the old data in this object...
//copy all the elements:
size = other.getSize();
for(size_t i = 0; i < size; ++i) {
DataElement* dat = new DataElement(*other.dataElements[i]);
dataElements[i] = dat;
}
}
Run Code Online (Sandbox Code Playgroud)
这不能编译,因为在const对象上不能使用dataElements [i].如何对const对象拥有的地图中的所有元素进行深层复制?
我知道在const映射上可以使用find()函数,但是如何获取我想要复制的实际对象?