我试着理解我是如何std::unique_ptr工作的,并且我找到了这个文件.作者从以下示例开始:
#include <utility> //declarations of unique_ptr
using std::unique_ptr;
// default construction
unique_ptr<int> up; //creates an empty object
// initialize with an argument
unique_ptr<int> uptr (new int(3));
double *pd= new double;
unique_ptr<double> uptr2 (pd);
// overloaded * and ->
*uptr2 = 23.5;
unique_ptr<std::string> ups (new std::string("hello"));
int len=ups->size();
Run Code Online (Sandbox Code Playgroud)
令我困惑的是,在这一行
unique_ptr<int> uptr (new int(3));
Run Code Online (Sandbox Code Playgroud)
我们使用整数作为参数(在圆括号之间)和这里
unique_ptr<double> uptr2 (pd);
Run Code Online (Sandbox Code Playgroud)
我们使用指针作为参数.它有什么不同吗?
对我来说还不清楚的是,以这种方式声明的指针将如何与以"正常"方式声明的指针不同.
C++标准库是否定义了此函数,还是必须使用Boost?
我搜索网络除了Boost之外找不到任何东西,但我想我最好问一下这里.
如果std::string支持移动语义,我应该通过值传递还是通过引用传递(到非内联函数)?那么使用小字符串优化(SSO)的实现呢?
为什么这段代码会导致Debug Assertion失败?
std::string query;
int ClientID = 666;
query = "select logged from login where id = ";
query.append((char *)ClientID);
Run Code Online (Sandbox Code Playgroud) 我正在编写我的编程任务的"驱动程序"部分,我不断得到这个荒谬的错误:
错误C2065:'cout':未声明的标识符
我甚至尝试过使用std :: cout,但我得到另一个错误:IntelliSense:命名空间"std"没有成员"cout"当我声明使用命名空间std,包括iostream +我甚至试图使用ostream
我知道这是一个标准的菜鸟问题,但这让我感到难过,我是一个新手(意思是:我之前已经编程了......)
#include <iostream>
using namespace std;
int main () {
cout << "hey" << endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我正在使用Visual Studio 2010并运行Windows 7.所有.h文件都有"using namespace std"并包含iostream和ostream.
是否有内置函数告诉我我的矢量包含某个元素,例如
std::vector<string> v;
v.push_back("abc");
v.push_back("xyz");
if (v.contains("abc")) // I am looking for one such feature, is there any
// such function or i need to loop through whole vector?
Run Code Online (Sandbox Code Playgroud) std :: map上[]运算符的声明是这样的:
T& operator[] ( const key_type& x );
Run Code Online (Sandbox Code Playgroud)
有没有理由不是这个?
T& operator[] ( const key_type& x );
const T& operator[] const ( const key_type& x );
Run Code Online (Sandbox Code Playgroud)
因为只要您需要在const方法中访问成员映射,这将非常有用.
我使用的是c ++ 11,还有一些没有为它配置的库,需要一些类型转换.特别是我需要一种方法转换std::__cxx11::string为常规std::string,但谷歌搜索我找不到一种方法来做到这一点,并放在(string)前面是行不通的.
如果我不转换,我会得到这样的链接器错误:
undefined reference to `H5::CompType::insertMember(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, unsigned long, H5::DataType const&) const'
Run Code Online (Sandbox Code Playgroud) #include <iostream>
#include <string>
#include <sstream>
#include "game.h"
#include "board.h"
#include "piece.h"
using namespace std;
Run Code Online (Sandbox Code Playgroud)
#ifndef GAME_H
#define GAME_H
#include <string>
class Game
{
private:
string white;
string black;
string title;
public:
Game(istream&, ostream&);
void display(colour, short);
};
#endif
Run Code Online (Sandbox Code Playgroud)
错误是:
game.h:8 error: 'string' does not name a type
game.h:9 error: 'string' does not name a type