Visual Studio 2010具有许多与C++标准相关的扩展(默认激活)/差异.
这种差异可能令人惊讶,并引发与其他行为不同的行为.VS因在模板代码验证方面非常宽松而着名,并且VS接受和编译的模板代码通常会被更兼容的编译器(CLang,Comeau,icc,gcc,......等等)彻底拒绝.
这个问题的目标是为这些差异提供参考(因此FAQ标签).
请根据差异(检查重复)和每个答案提供一个答案:
注意:C++ 0x是下一个标准,因此请避免列出C++ 0x扩展,因为它们很快就会成为标准
来自@Matteo Italia:Visual Studio合规性页面
我对c ++中的源文件和标题有疑问.
我有一个标题,用于声明函数.我在C++中实现这些功能.
现在我需要在其他文件中使用这些函数.我是否应该同时包含源文件和头文件才能使用这些功能?
在C++中,假设我有一个具有以下构造函数的Person -
Person::Person(const string& nm, const string& id)
{
name = nm;
idNum = id;
}
Run Code Online (Sandbox Code Playgroud)
现在,我还有一个名为Student的Person子类,其中包含额外的数据成员major和gradYear.是否有必要让Student类的构造函数在初始化列表中调用基类Person的构造函数,如果是,为什么?
Student::Student(const string& nm, const string& id, const string& maj, int year)
: Person(nm, id){
major =maj;
gradYear =year;
}
Run Code Online (Sandbox Code Playgroud)
我不能像这样定义学生的构造函数 -
Student::Student(const string& nm, const string& id, const string& maj, int year)
{
Person(nm, id);
major =maj;
gradYear =year;
}
Run Code Online (Sandbox Code Playgroud) 我是C++的新手,我想知道如何输出/写入声明为double的变量到txt文件.我知道如何使用fstream输出字符串,但我无法弄清楚如何发送任何其他内容.我开始认为你不能发送任何东西,但字符串到文本文件是正确的吗?如果是这样,那么如何将存储在变量中的信息转换为字符串变量?
这是我的代码,我正在尝试实现这个概念,它相当简单:
int main()
{
double invoiceAmt = 3800.00;
double apr = 18.5; //percentage
//compute cash discount
double discountRate = 3.0; //percentage
double discountAmt;
discountAmt = invoiceAmt * discountRate/100;
//compute amount due in 10 days
double amtDueIn10;
amtDueIn10 = invoiceAmt - discountAmt;
//Compute Interest on the loan of amount (with discount)for 20 days
double LoanInt;
LoanInt = amtDueIn10 * (apr /360/100) * 20;
//Compute amount due in 20 days at 18.5%.
double amtDueIn20;
amtDueIn20 = invoiceAmt * (1 + (apr …Run Code Online (Sandbox Code Playgroud) 假设我有一个autolocker类,看起来像这样:
template <T>
class autolocker {
public:
autolocker(T *l) : lock(l) {
lock->lock();
}
~autolocker() {
lock->unlock();
}
private:
autolocker(const autolocker&);
autolocker& operator=(const autolocker&);
private:
T *lock;
};
Run Code Online (Sandbox Code Playgroud)
显然,目标是能够将此autolocker与任何具有锁定/解锁方法的东西一起使用,而无需借助虚拟功能.
目前,它很简单,使用如下:
autolocker<some_lock_t> lock(&my_lock); // my_lock is of type "some_lock_t"
Run Code Online (Sandbox Code Playgroud)
但这样做是违法的:
autolocker lock(&my_lock); // this would be ideal
Run Code Online (Sandbox Code Playgroud)
反正有没有得到模板类型演绎与此相关(保持在我的autolocker是不可复制的).或者只是指定类型最简单?
我正在尝试使用智能指针,如auto_ptr,shared_ptr.但是,我不知道如何在这种情况下使用它.
CvMemStorage *storage = cvCreateMemStorage();
... use the pointer ...
cvReleaseMemStorage(&storage);
Run Code Online (Sandbox Code Playgroud)
我不确定,但我认为存储变量只是一个malloc的内存,而不是C++类对象.有没有办法使用智能指针存储变量?
谢谢.
我已经尝试将bool指针设置为特定的起始位置,然后逐步递增它.这会导致访问冲突.
我正在使用Visual Studio 2010
我正在查看一些c ++代码,并且在这种情况下不理解模板声明的目的:
template<> void operator>>(const ClassA& s, const ClassB d) {...}
Run Code Online (Sandbox Code Playgroud)
什么是语义template<>?
我有以下示例:
#include <stdio.h>
#include <map>
#include <conio.h>
typedef std::map<int,int> mi;
typedef std::map<int,int>::iterator mit;
mit myfind(mi mymap)
{
mit it = mymap.find(1);
printf("in function: %d\n",it->second);
return it;
}
void main()
{
mi a;
a.insert(std::pair<int,int>(1,2));
a.insert(std::pair<int,int>(3,4));
mit it = myfind(a);
printf("out of function: %d\n",it->second);
_getch();
}
Run Code Online (Sandbox Code Playgroud)
输出是:
功能:2
功能不足:-17891602
为什么?迭代器是否无效?为什么?提前致谢.
我编写了一个递归的Branch&Cut算法,现在正试图加快执行速度.我注意到了一些非常好奇的东西:有一次我调用一个函数来计算一些向量向量.使用clock(),我测量了在调用它的文件中以及在函数本身中花费的时间.可视化:
tic
foo(args);
time1 = toc
Run Code Online (Sandbox Code Playgroud)
和
void foo(args) {
tic
//do stuff
time2 = toc
}
Run Code Online (Sandbox Code Playgroud)
我的问题是time1大约是time2的3倍,这对我来说毫无意义.实际函数有很多参数,如下:
void allcomb( std::vector<int> &Vin,
std::vector<int> &Vprev,
Graph F,
int t,
const int n,
const int numofdests,
int Time_hor,
std::vector<int> &truckdest,
std::vector<int> &truck_dest_index,
std::vector<int> &descroflabel,
std::vector<int> &labelofdescr,
std::vector<std::vector<double> > &short_path_time,
std::vector<std::vector<double> > &short_path_fuel,
double eta,
std::vector<std::pair<int,int> >& next_hub,
std::vector<std::pair<double,double> >& distanceto_next_hub,
std::vector<std::vector<int> >& Choices )
Run Code Online (Sandbox Code Playgroud)
我通过引用传递所有向量以避免复制它们,但也许我错过了什么?或者通常使用那么多参数调用函数通常很慢?此外,进入该功能使用的时间比退出该功能的时间长,这可能很重要.
如果您需要更多信息,请告诉我.谢谢和欢呼,Christoph
提升Graph对象是问题,感谢发现它:)运行时间降低了10倍,完美!
c++ ×10
templates ×2
auto-ptr ×1
call ×1
constructor ×1
fstream ×1
function ×1
header ×1
iostream ×1
performance ×1
semantics ×1
shared-ptr ×1
stl ×1
winapi ×1
windows ×1