我最近开始使用Zend Studio,它报告了以下类型的代码:
$q = query("select * from some_table where some_condition");
while ($f = fetch($q)) {
// some inner workings
}
Run Code Online (Sandbox Code Playgroud)
要停止警告,代码需要像这样写:
$q = query("select * from some_table where some_condition");
$f = fetch($q);
while ($f) {
// some inner workings
$f = fetch($q);
}
Run Code Online (Sandbox Code Playgroud)
为什么这被标记为警告?这么糟糕吗?
我知道警告可能旨在阻止这样的错误:
$a = 1;
while ($a = 1) {
// some inner workings
$a++;
}
Run Code Online (Sandbox Code Playgroud)
永远不会终止,因为1被分配给$ a,而$ a又返回1到while语句,而不是针对$ a进行测试,并且当$ a不是1时,将false返回到while语句.
容易出错的错误可以验证警告,但是在第二个例子中忘记在while块的末尾添加额外的$ f = fetch($ q),这也会导致永远不会终止的循环.如果我更改我的代码以删除警告,然后忘记在while块的末尾添加$ f = fetch($ q),Zend将不会发出警告!
因此,通过删除有关常见错误的警告,我正在为自己设置一个不同的常见错误.
走出锅,进入火中.
warnings zend-framework variable-assignment while-loop conditional-statements
我搜索了如何在互联网上正确实现+运算符,我找到的所有结果都执行以下步骤:
const MyClass MyClass::operator+(const MyClass &other) const
{
MyClass result = *this; // Make a copy of myself. Same as MyClass result(*this);
result += other; // Use += to add other to the copy.
return result; // All done!
}
Run Code Online (Sandbox Code Playgroud)
关于这个"过程"我几乎没有问题:
以这种方式实现+运算符不是那么愚蠢,它在第一行调用赋值运算符(复制类),然后在返回中调用复制构造函数(由于返回的事实,它也复制了类)按价值,所以它破坏了第一个副本并创建了一个新的......坦白说,这并不是很聪明......)
当我写a = b + c时,b + c部分创建该类的新副本,然后'a ='部分将副本复制给自己.谁删除了b + c创建的副本?
有没有更好的方法来实现+运算符而不需要两次处理类,也没有任何内存问题?
提前致谢
class Node
{
public:
Node *parent; // used during the search to record the parent of successor nodes
Node *child; // used after the search for the application to view the search in reverse
float g; // cost of this node + it's predecessors
float h; // heuristic estimate of distance to goal
float f; // sum of cumulative cost of predecessors and self and heuristic
Node() :
parent( 0 ),
child( 0 ),
g( 0.0f ),
h( 0.0f ), …Run Code Online (Sandbox Code Playgroud) 这个问题与这个问题有关,但并不完全相同.
说我有这个数据框,
df <- data.frame(
id = c(1:6),
profession = c(1, 5, 4, NA, 0, 5))
Run Code Online (Sandbox Code Playgroud)
以及包含有关行业代码的人类可读信息的字符串.说,
profession.code <- c(
Optometrists=1, Accountants=2, Veterinarians=3,
`Financial analysts`=4, Nurses=5)
Run Code Online (Sandbox Code Playgroud)
现在,我正在寻找df$profession用文本中的文本替换值的最简单方法 profession.code.最好不使用特殊库,除非它显着缩短代码.
我希望我的最终结果是
df <- data.frame(
id = c(1:6),
profession = c("Optometrists", "Nurses",
"Financial analysts", NA, 0, "Nurses"))
Run Code Online (Sandbox Code Playgroud)
任何帮助将不胜感激.
谢谢,埃里克
有什么区别:
char fast_car[15]="Bugatti";
Run Code Online (Sandbox Code Playgroud)
和
char fast_car[15];
fast_car="Bugatti";
Run Code Online (Sandbox Code Playgroud)
因为第二个导致编译错误:
错误:从类型'char*'分配类型'char [15]'时出现不兼容的类型
虽然第一个工作正常.将数组中的字符串放在与数组初始化不同的位置会很有帮助.
我意识到差异可以忽略不计,但是在尝试将无符号长度归零时哪个更有效?
unsigned long x;
...
x=0;
--OR--
x^=x;
Run Code Online (Sandbox Code Playgroud)
泰勒
我有TestingID变量,在我的Java代码SQL字符串如下.稍后将使用sql字符串prepareStatement.
int TestingID;
String sqlInsert = "INSERT INTO TESTING VALUES(TESTING_SEQ.NEXTVAL, ?, ?)";
...
MethodA(TestingID); //passing TestingID to MethodA
Run Code Online (Sandbox Code Playgroud)
我需要将新插入的记录的下一个序列值放入其中,TestingID以便我可以在另一个方法中使用它,如上所示.
我有@obj.items_per_page,这是20在开始,我希望下面的方法只有在many_items不是nil:
def fetch_it_baby (many_items = nil)
@obj.items_per_page = many_items
Run Code Online (Sandbox Code Playgroud)
使用上面的代码,即使many_items是nil,@obj.items_per_page仍然是20.为什么?这是"好"的编码吗?我不应该使用类似的东西
@obj.items_per_page = many_items || @obj.items_per_page
Run Code Online (Sandbox Code Playgroud)
还是有第三种方式?无论哪种方式,我都感到不舒服.
考虑以下示例来说明问题(它只是为了解释问题而构建的,但我在书中看到了类似的代码以及实际项目中的代码):
package main
import (
"strconv"
"fmt"
"log"
)
func main() {
n1, err := strconv.Atoi("1")
if err != nil {
log.Panicf("%v", err)
}
n2, err := strconv.Atoi("2")
if err != nil {
log.Panicf("%v", err)
}
// err := fmt.Errorf("new error") <- line 1
// n1, err := strconv.Atoi("3") <- line 2
fmt.Printf("n1 = %d, n2 = %d\n", n1, n2)
}
Run Code Online (Sandbox Code Playgroud)
编译器不抱怨的重新定义err,但如果我取消<- line 1或者<- line 2,它会抱怨no new variable on left side of := …
我想知道我可以做什么类型的内省来检测可以通过原始内存副本分配的类型?
例如,据我所知,内置类型的内置类型元组和此类元组的元组将属于此类别.我的动机是,如果可能,我想传输原始字节.
T t1(...); // not necessarely default constructible
T t2(...);
t1 = t2; // should be equivalent to std::memcpy(&t1, &t2, sizeof(T));
// t1 is now an (independent) copy of the value of t2, for example each can go out of scope independently
Run Code Online (Sandbox Code Playgroud)
如果赋值可以(原则上)替换为?,那么在编译类型中可以告诉什么type_trait或组合?type_traitsmemcpy
我想你会为我猜应该fullfil这个条件的类型和让我吃惊的是适合的行为是不是唯一的工作,std::is_trivially_assignable但是std::trivially_destructible.这在某种程度上是有意义的,但我很困惑为什么其他一些选项不能用于预期的情况.
我知道可能没有防弹方法,因为人们总是可以编写一个有效可记忆的类,不能被"检测"为memcopyable,但我正在寻找一个适用于简单直观案例的类.
#include<type_traits>
template<class T> using trait =
std::is_trivially_destructible
// std::is_trivial
// std::is_trivially_copy_assignable
// std::is_trivially_copyable // // std::tuple<double, double> is not trivially copyable!!!
// std::is_trivially_default_constructible
// …Run Code Online (Sandbox Code Playgroud)