小编The*_* do的帖子

如何查找C++ std :: map中是否存在给定键

我正在尝试检查给定的密钥是否在地图中,有些不能这样做:

typedef map<string,string>::iterator mi;
map<string, string> m;
m.insert(make_pair("f","++--"));
pair<mi,mi> p = m.equal_range("f");//I'm not sure if equal_range does what I want
cout << p.first;//I'm getting error here
Run Code Online (Sandbox Code Playgroud)

那么如何打印p中的内容呢?

c++ dictionary stl

412
推荐指数
9
解决办法
50万
查看次数

如何清除stringstream?

stringstream parser;

parser << 5;
short top = 0;
parser >> top;
parser.str(""); //HERE I'M RESETTING parser

parser << 6; //DOESN'T PUT 6 INTO parser
short bottom = 0;
parser >> bottom;
Run Code Online (Sandbox Code Playgroud)

为什么不起作用?

c++ stringstream

113
推荐指数
1
解决办法
8万
查看次数

中止,终止或退出?

这三者之间有什么区别,如果我无法正常处理异常,我该如何结束程序呢?

c++

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

显式实例化 - 何时使用?

几个星期休息之后,我正在尝试使用David Vandevoorde和Nicolai M. Josuttis 所着的模板 - 完整指南来扩展和扩展我的模板知识,我现在想要了解的是模板的显式实例化.

我实际上并没有这样的机制问题,但我无法想象我想要或想要使用此功能的情况.如果有人能向我解释,我将不仅仅是感激.

c++ templates

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

Ctor不允许返回类型

有代码:

struct B
{
    int* a;
    B(int value):a(new int(value))
    {   }
    B():a(nullptr){}
    B(const B&);
}

B::B(const B& pattern)
{

}
Run Code Online (Sandbox Code Playgroud)

我收到错误消息:
'错误1错误C2533:'B :: {ctor}':构造函数不允许返回类型'

知道为什么吗?
PS我正在使用VS 2010RC

c++ constructor

41
推荐指数
2
解决办法
3万
查看次数

我应该优化还是让编译器这样做?

根据效率编写循环的首选方法是什么:方式a)

   /*here I'm hoping that compiler will optimize this  
 code and won't be calling size every time it iterates through this loop*/
    for (unsigned i = firstString.size(); i < anotherString.size(), ++i)
    {
    //do something
    }
Run Code Online (Sandbox Code Playgroud)

或者我应该这样做:方式b)

unsigned first = firstString.size();
unsigned second = anotherString.size();
Run Code Online (Sandbox Code Playgroud)

现在我可以写:

    for (unsigned i = first; i < second, ++i)
    {
    //do something
    }
Run Code Online (Sandbox Code Playgroud)

第二种方式在我看来更糟糕的选择有两个原因:范围污染和冗长,但它的优点是确保每个对象都会调用一次size().
期待您的回答.

c++ optimization performance

25
推荐指数
5
解决办法
1521
查看次数

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

功能模板的部分特化

有谁知道,在C++ 11中,函数模板是否可以部分专用?

c++ templates partial-specialization c++11

20
推荐指数
1
解决办法
4469
查看次数

模板外的typename

这在VS2010sp1中没有编译(虽然它使用gcc 4.6进行编译):

template<class T>
struct Upcast;

template<>
struct Upcast<signed char>
{
    typedef signed short type;
};

template<>
struct Upcast<char>
{
    typedef typename std::conditional<std::is_signed<char>::value,short, unsigned short>::type type;
};

int main()
{
    Upcast<char>::type a;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

来自VS的错误:

Error   1   error C2899: typename cannot be used outside a template declaration
Run Code Online (Sandbox Code Playgroud)

哪支队伍是对的?VS还是gcc?

c++ templates

20
推荐指数
2
解决办法
9577
查看次数

将编译器添加到code :: blocks

在此输入图像描述这里我下载了最新版本的GCC,即4.6.
如何将此编译器添加到我的code :: blocks中?

compiler-construction gcc

18
推荐指数
1
解决办法
7万
查看次数