我想知道是否有可能以及如何在C++ crossplatform中实现(C99,C++ 0x独立)snprintf?有没有这样的提升?(我想知道要替换的C++习语是snprintf(4)什么?)
我正在尝试使用编译库-std=c++03,但编译失败,因为nullptr_t未定义.
如何使用硬编码宏保证C++ 03而不是C++ 11编译?
提前致谢.
示例代码:
class Thingy
{
public:
void doStuff(std::string&);
std::string doStuff();
};
void Thingy::doStuff(std::string& str) {
str = "stuff";
}
std::string Thingy::doStuff() {
return "stuff";
}
int main(int argc, const char* args[])
{
std::string name;
Thingy thingy;
thingy.doStuff(name);
std::cout << name << " " << thingy.doStuff() << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
特别是对于字符串,要么是方法更有效,那么效率"值得"来扭曲代码的可读性或一致性?(例如,如果我不需要,我宁愿不创建名称变量)
事实上,这是(在一天结束时)与此讨论相关的字符串常量吗?
在我的C++代码中,我有一个Matrix类,并且编写了一些运算符来乘以它们.我的类是模板化的,这意味着我可以有int,float,double ...矩阵.
我猜运算符过载是经典的
template <typename T, typename U>
Matrix<T>& operator*(const Matrix<T>& a, const Matrix<U>& b)
{
assert(a.rows() == b.cols() && "You have to multiply a MxN matrix with a NxP one to get a MxP matrix\n");
Matrix<T> *c = new Matrix<T>(a.rows(), b.cols());
for (int ci=0 ; ci<c->rows() ; ++ci)
{
for (int cj=0 ; cj<c->cols() ; ++cj)
{
c->at(ci,cj)=0;
for (int k=0 ; k<a.cols() ; ++k)
{
c->at(ci,cj) += (T)(a.at(ci,k)*b.at(k,cj));
}
}
}
return *c;
}
Run Code Online (Sandbox Code Playgroud)
在这段代码中,我返回一个与第一个参数相同类型的矩阵,即Matrix<int> * Matrix<float> …
我正在可视化Mandelbrot集以及其他一些分形,并且有很多重复的代码,但是没有代码重用。
我正在使用的功能之一如下:
/**
* determines whether a pixel lies in the set
* @params x, y - x and y coordinates on R/I axes
* @param c - a complex number
*/
void calculateSet(int x, int y, Complex c) {
Complex z = c.clone();
int n = 0;
for (; n < maxDepth; n++) {
if (z.dis() > 4) { break; }
z = z^2 + c;
}
// some code using n to color the set
}
Run Code Online (Sandbox Code Playgroud)
这遵循Mandelbrot集:
z_(n+1) …Run Code Online (Sandbox Code Playgroud) #include <vector>
class M {
public:
M(unsigned int);
unsigned int n;
};
M::M(unsigned int i) { n = i; }
class A {
protected:
char t;
public:
virtual ~A();
virtual std::vector<M> foo(unsigned int);
char getChar();
};
A::~A(){}
std::vector<M> A::foo(unsigned int u) {
std::vector<M> v;
return v;
}
char A::getChar() { return t; }
class B : public A {
public:
B();
std::vector<M> foo(unsigned int);
};
B::B() { t = 'b'; }
std::vector<M> B::foo(unsigned int c) {
std::vector<M> v;
for (unsigned …Run Code Online (Sandbox Code Playgroud) 我有这个多维2阵列
int anArray1[MAX_ROW][MAX_CELL] =
{
{ 0, 1, 1, 0, 1, 1, 1, 0},
{ 0, 1, 1, 0, 1, 1, 1, 0},
{ 0, 1, 1, 0, 1, 1, 1, 0},
{ 0, 1, 1, 0, 1, 1, 1, 0},
{ 0, 0, 0, 0, 0, 0, 0, 0}
}
int anArray2[MAX_ROW][MAX_CELL] =
{
{ 0, 1, 1, 0, 1, 1, 1, 0},
{ 0, 1, 1, 0, 1, 1, 1, 0},
{ 0, 1, 1, 0, 1, 1, …Run Code Online (Sandbox Code Playgroud) c++ containers multidimensional-array c++03 visual-studio-2012
假设我有以下枚举.
enum VehicleType
{
CAR,
PLANE,
UNDEFINED
};
Run Code Online (Sandbox Code Playgroud)
现在在Vehicle中(想象一个具有VehicleType变量的简单类); 默认构造函数我必须为车辆分配一些车型.不知道它是什么,我不想把它称为汽车或飞机,所以我称之为未定义.
这会给枚举用户带来负担,因为他们必须考虑如何处理未定义的类型.如果我想摆脱UNDEFINED,我在C++ 03中的选择是什么?
我可以考虑重新设计,以便默认构造函数根本不可用,但由于大局,这似乎不太可行.
编辑:这个问题比问题复杂得多.但问题的答案是正确的,所以我会接受.:)
c++ ×8
c++03 ×8
boost ×1
c ×1
c++11 ×1
class ×1
containers ×1
enums ×1
expression ×1
macros ×1
polymorphism ×1
templates ×1
virtual ×1