如果我不初始化i,我不知道它的价值,但我也无法预测它的价值rand().
但另一方面,我知道未初始化的价值i在INT_MIN和之间INT_MAX,而且我也知道rand()在0和之间的价值RAND_MAX.
为什么使用未初始化的i未定义行为的值但使用的值rand()不是?
例如,我有一个具有保留计数的类和一个可以在保留计数为0时删除self的释放方法:
class MyClass{
public:
void retain(){
this->retainCount++;
}
void release(){
this->retainCount--;
if(this->retainCount==0){
delete this;
}
printf("release called");
MyClass::deleteCount++;
FileOutputStream* fio=new FileOutputStream();
fio->generateLog();
delete fio;
EmailUtils::sendEmailAboutMemoryUsage();
}
protected:
int retainCount;
static int deleteCount;
}
Run Code Online (Sandbox Code Playgroud)
在删除对象后,我可能会有一些代码要做:
printf("release called");
MyClass::deleteCount++;
FileOutputStream* fio=new FileOutputStream();
fio->generateLog();
delete fio;
EmailUtils::sendEmailAboutMemoryUsage();
Run Code Online (Sandbox Code Playgroud)
我的问题是,如果删除后的代码块不需要任何访问,那么在删除对象后继续执行代码是否安全?
例如,我有一个游戏,其中一些角色有5个元素:火,水,木,光,黑.
元素有弱点,某些元素的攻击力会增加:
火>木
水>火
木材>水
光>暗
暗>光
例如:
火击木头,力量是*2
火打水,功率为*0.5
我有一个函数来获取元素和因子之间的关系:
float getFactor(string me,string enemy){
if(me=="fire"){
if(enemy=="fire"){
return 1;
}else if(enemy=="water"){
return 0.5;
}else if(enemy=="wood"){
return 2;
}else if(enemy=="light"){
return 1;
}else{
return 1;
}
}else if(me=="water"){
if(enemy=="fire"){
return 2;
}else if(enemy=="water"){
return 1;
}else if(enemy=="wood"){
return 0.5;
}else if(enemy=="light"){
return 1;
}else{
return 1;
}
}else if(me=="wood"){
if(enemy=="fire"){
return 0.5;
}else if(enemy=="water"){
return 2;
}else if(enemy=="wood"){
return 1;
}else if(enemy=="light"){
return 1;
}else{
return 1;
}
}else if(me=="light"){
if(enemy=="fire"){ …Run Code Online (Sandbox Code Playgroud) 在我的印象中,我似乎看到了类似的东西:
A:::b()
Run Code Online (Sandbox Code Playgroud)
但是我不记得它是不是c ++,也是在c ++中搜索过"三重冒号"之后但是在c ++中看起来很少有关于它的信息,有没有例子代码可以在代码中有3个冒号(:: :)有效的语法?
例如,我可以在编译时检查N是否> 0,如下所示:
#include <stdio.h>
template<int N>
struct Is{
enum{Positive=N>0?1:0};
};
template<>
struct Is<0>{
};
int main(){
printf("%d\n",Is<3>::Positive);
printf("%d\n",Is<-3>::Positive);
return 0;
};
Run Code Online (Sandbox Code Playgroud)
哪个过滤器0强制Is <0> :: Positive无法编译,但有没有任何方法(例如:模板,宏....)强制Is <N> ::正当N不是> 0时无法编译?
#include <sstream>
#include <vector>
using namespace std;
bool myfunc(bool* i,bool* j){
return *i<*j;
}
int main(){
vector<bool> a;
a.push_back(true);
a.push_back(false);
a.push_back(false);
a.push_back(true);
a.push_back(true);
vector<bool*> aa;
for(int i=0;i<a.size();i++){
aa.push_back(&a[i]);
}
sort(aa.begin(),aa.end(),myfunc);
for(int i=0;i<aa.size();i++){
printf("%d\n",*(aa[i]));
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
但它不能编译并说:
no viable conversion from '__bit_iterator<std::__1::vector<bool, std::__1::allocator<bool> >, false>' to 'const value_type' (aka 'bool *const')
aa.push_back(&a[i]);
Run Code Online (Sandbox Code Playgroud)
但我不敢相信它有错误的逻辑代码,因为我记得我可以用int做一些类似的工作,所以我尝试将一些bool转换为整数:
#include <sstream>
#include <vector>
using namespace std;
int myfunc(int* i,int* j){
return *i<*j;
}
int main(){
vector<int> a;
a.push_back(true);
a.push_back(false);
a.push_back(false);
a.push_back(true);
a.push_back(true);
vector<int*> aa;
for(int …Run Code Online (Sandbox Code Playgroud) 一方面,Math.random()或new Random().nextFloat()应返回介于0和1之间的值,但不包括1,另一方面,浮点数可能不准确,值可能会在实际值附近变化.是否暗示Math.random()或new Random().nextFloat()可能在实际中返回值<0或> = 1?如果没有,为什么不会发生?
我在应用程序中有一些可下载的内容(例如:游戏角色图像),在 iOS 中它们被保存到路径 NSCachesDirectory,我遵循 iOS 的指导方针,可下载内容应保存在 NSCachesDirectory 中,这是一个缓存目录。如果我应该将文件保存到 android 端的 getCacheDir() 中,我会很挣扎。
起初,我认为 iOS 中的缓存文件夹应该与 android 中的缓存文件夹等效(或相似),但在查看了一些文档后,我怀疑它们在功能上是否相同:
1.iOS 建议将一些可下载的内容保存在缓存文件夹中,此类内容更多是在 tmp 中持久化临时文件。但是在 android 中,文档说 getCacheDir() 应该存储缓存文件而不是持久文件,这让我怀疑 android 缓存文件夹更像 iOS 中的 tmp 文件夹
2.我在iOS缓存文件上找不到任何大小限制指南,但android方面说它应该有合理的大小限制,比如1 MB(我认为我的内容会远远超过1 MB,比如50到100 MB)。
所以我的问题是,iOS 缓存文件夹在功能上是否等同于 android 中的缓存文件夹?在我在 iOS 端使用 NSCachesDirectory 的地方在 android 端使用 getCacheDir() 是否正确?如果没有,当我在 iOS 端使用 NSCachesDirectory 时,我应该在 android 端使用哪个路径?
例如:
for(int i=0;i<v.size();i++){
}
Run Code Online (Sandbox Code Playgroud)
是正常的顺序,
for(int i=v.size()-1;i>=0;i--){
}
Run Code Online (Sandbox Code Playgroud)
是逆序,
如何反转迭代版本的迭代器?
for(vector<int>::iterator it=v.begin();it!=v.end();++it){
}
Run Code Online (Sandbox Code Playgroud)
还有这个代码风格的反向版本?
for(int i : v){
}
Run Code Online (Sandbox Code Playgroud) 例如,我可以将数组硬编码为参数:
void test(pair<string,int> v[],int size){
for(int i=0;i<size;i++){
printf("%s %d\n",v[i].first.c_str(),v[i].second);
}
}
int main(){
test((pair<string,int>[]){make_pair("a",1),make_pair("b",2)},2);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
所以我不需要创建pair v []的临时变量,然后不需要担心temp变量的变量名,如果使用vector,是否有类似的语法:
void test(vector<pair<string,int> > v){
for(pair<string,int> p : v){
printf("%s %d\n",p.first.c_str(),p.second);
}
}
Run Code Online (Sandbox Code Playgroud)
?
我想创建一个函数,它根据元素的值随机返回向量索引作为概率:
float getRandomIndex(const vector<float>& v){
random_device r;
mt19937 m(r());
return discrete_distribution<>(v.begin(),v.end())(m);
}
int main(){
for(int i=0;i<10;i++){
vector<float> a{0.9,0.1};
cout << getRandomIndex(a) << endl;
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
现在我想减少行号,所以尝试将函数重写为:
float getRandomIndex(const vector<float>& v){
mt19937 m(random_device()());
return discrete_distribution<>(v.begin(),v.end())(m);
}
Run Code Online (Sandbox Code Playgroud)
但编译失败:
error: function cannot return function type 'std::__1::random_device ()'
mt19937 m(random_device()());
^
warning: parentheses were disambiguated as a function declaration [-Wvexing-parse]
mt19937 m(random_device()());
^~~~~~~~~~~~~~~~~~~
note: add a pair of parentheses to declare a variable
mt19937 m(random_device()());
^
(
Run Code Online (Sandbox Code Playgroud)
生成1个警告和1个错误.
但我会尝试
random_device r;
cout …Run Code Online (Sandbox Code Playgroud) 例如:
6+1.2=6*1.2
5+1.25=5*1.25
Run Code Online (Sandbox Code Playgroud)
我想尝试编写一个程序来搜索结果,如搜索素数:
public class Test{
public static void main(String[] args){
float x=-1.0f;
float d=0.0001f;
for(float y=-10;y<10;y=y+d){
if(x+y-x*y>=-d && x+y-x*y<=d){
System.out.println(y);
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
但程序有一些问题:
Float.MIN_VALUE和Float.MAX_VALUE太慢有没有其他更快的方法或更好的算法来查找结果?