我曾经认为,随着std::vector::operator[]我们获得被访问项目的深层副本,但似乎并非总是如此.至少,使用vector<bool>以下测试代码会得到不同的结果:
#include <iostream>
#include <vector>
using namespace std;
template <typename T>
void Test(const T& oldValue, const T& newValue, const char* message)
{
cout << message << '\n';
vector<T> v;
v.push_back(oldValue);
cout << " before: v[0] = " << v[0] << '\n';
// Should be a deep-copy (?)
auto x = v[0];
x = newValue;
cout << " after: v[0] = " << v[0] << '\n';
cout << "-------------------------------\n";
} …Run Code Online (Sandbox Code Playgroud) 什么是C++ 11的一些std::unique_ptr用途和陷阱?
我std::unique_ptr还可以使用它来存储动态分配的数组吗?
我std::unique_ptr可以使用自定义删除机制使用资源吗?
如果我需要多态性,我应该使用原始指针而不是unique_ptr?
我看到一些线程显示如何使用unique_ptr多态行为.我不确定这是值得的麻烦,我宁愿留下原始指针.在这种情况下,您能否对此进行评论,您对原始指针和智能指针的看法?
MSDN显示以下示例代码段vsnprintf_s:
Run Code Online (Sandbox Code Playgroud)// crt_vsnprintf_s.cpp #include <stdio.h> #include <wtypes.h> void FormatOutput(LPCSTR formatstring, ...) { int nSize = 0; char buff[10]; memset(buff, 0, sizeof(buff)); va_list args; va_start(args, formatstring); nSize = vsnprintf_s( buff, _countof(buff), _TRUNCATE, formatstring, args); printf("nSize: %d, buff: %s\n", nSize, buff); } int main() { FormatOutput("%s %s", "Hi", "there"); FormatOutput("%s %s", "Hi", "there!"); FormatOutput("%s %s", "Hi", "there!!"); }
在此示例中,va_start调用时没有匹配va_end.
这是MSDN中的doc bug,还是应该在调用va_start 之前调用vsnprintf_s然后让这个函数va_end为我们进行清理(即调用)?
BTW:我尝试了上面的代码,它与VS2015一起使用Update 3,但我不知道它是否只是未定义的行为......
我有一个简单的类结构建模离散模拟,带有状态向量,每个状态包含许多过渡,作为智能指针的向量.我已经使用智能指针来保存转换,就像在我的完整应用程序中我需要多态性一样.
#include <vector>
#include <memory>
class Transition {
public:
Transition() {}
};
class State {
public:
State(int num) : num(num), transitions() {}
void add_transition(std::unique_ptr<Transition> trans) {
transitions.push_back(std::move(trans));
}
private:
int num;
std::vector<std::unique_ptr<Transition>> transitions;
};
int main() {
std::vector<State> states;
for (int i = 0; i < 10; i++) {
State nstate = State(i);
for (int j = 0; j < 2; j++) {
nstate.add_transition(std::move(std::unique_ptr<Transition>(new Transition())));
}
// This line causes compiler errors
states.push_back(nstate);
}
}
Run Code Online (Sandbox Code Playgroud)
将新状态对象添加到向量时,我遇到编译器错误:
Error: use of deleted …Run Code Online (Sandbox Code Playgroud) 我怎样才能将窄幅转换string为宽幅string?
我试过这个方法:
string myName;
getline( cin , myName );
wstring printerName( L(myName) ); // error C3861: 'L': identifier not found
wchar_t* WprinterName = printerName.c_str(); // error C2440: 'initializing' : cannot convert from 'const wchar_t *' to 'wchar_t *'
Run Code Online (Sandbox Code Playgroud)
但我得到上面列出的错误.
为什么我会收到这些错误?我该如何解决它们?
有没有其他方法可以直接将narrow字符串转换为wide字符串?
我正在阅读以前编写的代码,并找到了StringCbPrintf()函数
我在msdn网站上发现了这样的声明:
HRESULT StringCbPrintf(
_Out_ LPTSTR pszDest,
_In_ size_t cbDest,
_In_ LPCTSTR pszFormat,
_In_ ...
);
Run Code Online (Sandbox Code Playgroud)
什么是_in_和_out_这里?
为什么我们已经拥有它需要它sprintf()?
当在a中存储大量自定义类的实例(不是"简单"类,例如不是a std::string,而不是a std::complex等)时std::vector,我们应该选择一个简单的std::vector<X>,还是std::vector<std::unique_ptr<X>>更好的选择?
我写了一些基准代码(从这篇关于C++ 03的C++ 11移动语义改进的博客文章中扩展代码),似乎vector<unique_ptr<X>>为1,500,000项向量提供了更好的性能.事实上,在装有Windows 7 64位,Intel Core i5四核CPU和8 GB RAM的PC上,我得到了以下结果(test.exe 1500):
vector<unique_ptr<MyObject>>:1.5秒vector<shared_ptr<MyObject>>:1.6秒vector<MyObject>:1.8秒所以,在C++ 03中(哪里std::unique_ptr不可用),似乎最好的选择是vector<shared_ptr<X>>; 而在C++ 11中,支持move-semantics的std::unique_ptr似乎提供了最好的结果.
我在这里错过了什么吗?这是一个很好的C++指南,在大vectors中最好存储(智能)指针类实例而不是类实例本身吗?
基准代码如下:
////////////////////////////////////////////////////////////////////////////////
//
// Test vector<X> vs. vector<unique_ptr<X>> vs. vector<shared_ptr<X>>.
//
// Original benchmark code from:
// http://blogs.msdn.com/b/vcblog/archive/2009/06/23/stl-performance.aspx
//
////////////////////////////////////////////////////////////////////////////////
#include <exception> // std::invalid_argument
#include <iostream> // std::cout …Run Code Online (Sandbox Code Playgroud) observer_ptr移动操作后为什么不归零?
它nullptr在默认构造中正确设置,这确实有意义(并防止指向垃圾).
,因此,它应该被清零时,std::move()从"d一样std::string,std::vector等等.
这将使它成为原始指针有意义的几个上下文中的一个很好的候选者,以及在具有原始指针数据成员的类上自动生成移动操作,就像在这种情况下一样.
编辑
正如@JonathanWakely在评论中指出的那样(这与上述问题有关):
如果
observer_ptr在移动后为null,则可以使用它来为具有指针成员的类型实现Zero of Zero.这是一个非常有用的功能.
考虑编写一个可重用的自定义函数,它在其体内创建COM对象并调用某些COM接口的方法.为了使其正常工作,必须调用CoInitializeEx匹配的CoUninitializeAPI.
调用这些COM初始化和清理API的内部函数的身体会躲在一个COM实现细节给调用者,并会删除从主叫方的负担也是如此.
但是调用CoInitializeEx和匹配的CoUninitialize内部函数的主体被认为是一个很好的编码实践吗?
在函数粒度级别调用那些COM初始化/清理函数是否意味着每个函数调用的开销过多?
这个设计还有其他缺点吗?
c++ ×10
c++11 ×6
c ×3
vector ×3
pointers ×2
winapi ×2
auto ×1
boolean ×1
com ×1
move ×1
performance ×1
polymorphism ×1
printf ×1
string ×1
unicode ×1
unique-ptr ×1
visual-c++ ×1
windows ×1