我正在尝试检查给定的密钥是否在地图中,有些不能这样做:
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中的内容呢?
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)
为什么不起作用?
几个星期休息之后,我正在尝试使用David Vandevoorde和Nicolai M. Josuttis 所着的模板 - 完整指南来扩展和扩展我的模板知识,我现在想要了解的是模板的显式实例化.
我实际上并没有这样的机制问题,但我无法想象我想要或想要使用此功能的情况.如果有人能向我解释,我将不仅仅是感激.
有代码:
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
根据效率编写循环的首选方法是什么:方式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().
期待您的回答.
数组是否默认通过ref或value传递?谢谢.
有谁知道,在C++ 11中,函数模板是否可以部分专用?
这在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?
从这里我下载了最新版本的GCC,即4.6.
如何将此编译器添加到我的code :: blocks中?
c++ ×9
templates ×3
arrays ×1
c++11 ×1
constructor ×1
dictionary ×1
gcc ×1
optimization ×1
performance ×1
pointers ×1
stl ×1
stringstream ×1