条件检查:
if denominator == 0:
// do something like informing the user, or skipping this iteration.
else:
result = numerator/denominator
if FileExists('path/to/file'):
// open file read & write.
else:
// do something like informing the user, or skipping this iteration.
Run Code Online (Sandbox Code Playgroud)
异常处理:
try:
result = numerator/denominator
catch (DevidedByZeroException):
//take action
try:
//open file read & write.
catch (FileNotExistsException):
//take action
Run Code Online (Sandbox Code Playgroud)
我经常遇到这样的情况.哪一个去?为什么?
我在使用声明方面所做的大部分研究,包括阅读各种样式指南的相关部分,表明在C++源文件中使用声明是否使用,只要它们出现在所有#includes之后,就是决定留给编码员.即使是我阅读过的风格指南,为了保持一致性,这些风格指南通常也会出现在一方或另一方面,但在这方面相当灵活.
我的问题是,鉴于这种高度的灵活性,使用一致的风格有多重要?例如,假设作者写了类似的东西
using std::vector;
vector<T> v;
std::cout << v[0] << std::endl;
Run Code Online (Sandbox Code Playgroud)
是否在std :: vector上使用不一致的应用程序而不是std :: cout或std :: endl通常被认为是可接受的,还是被认为是无纪律的?
鉴于代码:
#include <iostream>
using namespace std;
template <typename T>
T my_max (const T &t1, const T &t2)
{
static int counter = 0;
counter++;
cout << counter << " ";
return ((t1 > t2) ? t1 : t2);
}
int main()
{
my_max (2,3);
my_max (3.5, 4.3);
my_max (3,2);
my_max ('a','c');
}
Run Code Online (Sandbox Code Playgroud)
输出是:
1 1 2 1
Run Code Online (Sandbox Code Playgroud)
我知道静态成员只初始化一次.我的问题是编译器如何记住调用该泛型函数的类型?幕后实际发生了什么?
我有两个课程Base,Derived从中:
class Base{
public:
Base(int = 0);
Base(Base&);
Base& operator=(const Base&);
protected:
int protectedData;
private:
int baseData;
};
/////////////DERIVED CLASS
class Derived: public Base{
public:
Derived(int = 0);
Derived(Derived&);
Derived(Base&);
Derived& operator=(const Derived&);
private:
int derivedData;
};
Run Code Online (Sandbox Code Playgroud)
///////////BASE FUNCTIONS
Base::Base(int value): protectedData(value), baseData(value)
{
cout << "base C'tor" << endl;
}
Base::Base(Base& base)
{
baseData = base.baseData;
protectedData = base.protectedData;
cout << "base Copy C'tor" << endl;
}
Base& Base::operator=(const Base& base)
{
if(this == …Run Code Online (Sandbox Code Playgroud) 考虑以下代码:
template <typename T>
class String
{
public:
...
String(T* initStr)
{
size_t initStrLen;
if (initStr != NULL)
{
printf_s("%s\n", typeid(T) == typeid(char) ? "char" : "wchar_t");
if (typeid(T) == typeid(char))
{
strlen((T*)initStr);
}
else if (typeid(T) == typeid(wchar_t))
{
wcslen((T*)initStr);
}
}
}
...
};
Run Code Online (Sandbox Code Playgroud)
当我编译代码时,我收到此错误消息:
...\main.cpp(32):错误C2664:'strlen':无法将参数1从'wchar_t*'转换为'const char*'
然后我尝试使用函数指针:
typedef size_t (*STRLEN)(void*);
STRLEN _strlen;
_strlen = reinterpret_cast<STRLEN> (typeid(*initStr) == typeid(char) ? strlen : wcslen);
Run Code Online (Sandbox Code Playgroud)
并且编译器再次发出错误,这次:
...\main.cpp(28):错误C2446:':':没有从'size_t(__ cdecl*)(const wchar_t*)'转换为'size_t(__ cdecl*)(const char*)'
我的问题是,我如何使用这些功能strlen和wcslen模板?
假设a = [5; 4; 3; 2; 1]并且我想要所有条目> 3,所以我希望它吐出v = [5,4].
我知道"find"只能找到索引,所以它并不完全有效.
有什么建议?
这是我的代码
// replace all new lines with string "nl"
std::string s = "Stack\nover\rflowâ€";
boost::regex expr1("(\\n)|(\\r)");
std::string fmt("nl");
std::string s2 = boost::regex_replace(s, expr, fmt);
Run Code Online (Sandbox Code Playgroud)
然后用空字符串替换所有非ascii字符
boost::regex expr2("([^\x20-\x7E])")
std::string fmt2("");
std::cout << boost::regex_replace(s2, expr2, fmt2) << std::endl;
Run Code Online (Sandbox Code Playgroud)
我宁愿打一个电话来代替两个.
有没有简单的方法来使用零填充来连接具有不等维度的矩阵?
short = [1 2 3]';
long = [4 5 6 7]';
desiredResult = horzcat(short, long);
Run Code Online (Sandbox Code Playgroud)
我想要像:
desiredResult =
1 4
2 5
3 6
0 7
Run Code Online (Sandbox Code Playgroud) 在我看来,boost scoped_ptr和shared_ptr中的reset方法导致构造和销毁的错误顺序:
boost::scoped_ptr<Component> component(GetDefaultComponent());
component.reset(new BetterComponent); // 1. Creation of the new object
// 2. Destruction of the old object
Run Code Online (Sandbox Code Playgroud)
这是IMO的错误订单.
可以先调用不带参数的reset方法,然后设置新指针.然而,这对我来说似乎是一种解决方法.(它是一种"解决方法"意味着存在错误.)
我相信,提振人非常聪明.因此,目前的方法必须有理由.
有谁知道更多?
我想创建一个环境,它是我的root环境的精确副本,但不制作任何包的硬拷贝(稍后我将添加一些不在 Anaconda 中的包)。我想我可以通过以下之一来做到这一点:
conda create -n newroot --clone root
conda create -n newroot --copy root
conda create -n newroot anaconda
Run Code Online (Sandbox Code Playgroud)
但这些都是下载包。如何创建当前 Anaconda 发行版的精确副本环境?(我知道以后我可以添加软件包conda install -n newroot <package name>)