标签: std

如何声明std :: unique_ptr以及它的用途是什么?

我试着理解我是如何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++ pointers std unique-ptr

76
推荐指数
3
解决办法
11万
查看次数

std :: lexical_cast - 有这样的事吗?

C++标准库是否定义了此函数,还是必须使用Boost?

我搜索网络除了Boost之外找不到任何东西,但我想我最好问一下这里.

c++ parsing casting std c++-standard-library

72
推荐指数
5
解决办法
5万
查看次数

通过Value或Reference传递std :: string

可能重复:
传递const std :: string&作为参数的日期是多少?

如果std::string支持移动语义,我应该通过值传递还是通过引用传递(到非内联函数)?那么使用小字符串优化(SSO)的实现呢?

c++ string std move-semantics

72
推荐指数
3
解决办法
11万
查看次数

将int附加到std :: string

为什么这段代码会导致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)

c++ std

69
推荐指数
3
解决办法
15万
查看次数

错误C2065:'cout':未声明的标识符

我正在编写我的编程任务的"驱动程序"部分,我不断得到这个荒谬的错误:

错误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.

c++ namespaces std visual-studio-2010-beta-2

67
推荐指数
5
解决办法
29万
查看次数

在C++中检查std :: vector <string>是否包含某个值

是否有内置函数告诉我我的矢量包含某个元素,例如

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)

c++ vector std stdvector

67
推荐指数
4
解决办法
16万
查看次数

为什么std :: map没有const访问器?

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++ std c++11

67
推荐指数
4
解决办法
6038
查看次数

什么是"你好,世界!" "std :: ref"的例子?

有人可以举一个简单的例子来证明其功能std::ref吗?我的意思是一个例子,其中只有在std::ref没有它们的情况下无法解释时才使用其他一些结构(如元组或数据类型模板).

我在std::ref 这里这里找到了两个问题.但是在第一个中,它涉及编译器中的错误,而在第二个中,使用std::ref不包含的示例,std::ref它们涉及元组和数据类型模板,这使得对这些示例的理解变得复杂.

c++ std ref c++11

66
推荐指数
3
解决办法
8970
查看次数

将std :: __ cxx11 :: string转换为std :: string

我使用的是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)

c++ string types std c++11

66
推荐指数
4
解决办法
8万
查看次数

为什么我得到字符串没有命名类型错误?

game.cpp

#include <iostream>
#include <string>
#include <sstream>
#include "game.h"
#include "board.h"
#include "piece.h"

using namespace std;
Run Code Online (Sandbox Code Playgroud)

game.h

#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

c++ string std

64
推荐指数
4
解决办法
22万
查看次数