在C++中执行此操作
char* cool = "cool";
Run Code Online (Sandbox Code Playgroud)
编译好,但给了我一个警告:
不推荐将字符串常量转换为char*.
我永远不会故意使用C风格的字符串std::string,但以防我问这个问题:
在没有const修饰符的情况下声明C风格的字符串是不好的做法?如果是这样,为什么?
我想在运行SDL 2程序时在控制台上打印一些调试内容,但这似乎是不可能的。printf("Hi!\n")而SDL_Log("Hi!\n")两者不会做我任何好处。
我什至在初始化SDL之前(以及退出它之后)都尝试过打印,但无济于事。似乎仅导入 SDL库就不可能将任何内容打印到控制台。
这是我正在编译的参数,因为可能与它有关:
g++ hello.cc -IC:\mingw_dev_lib\include\SDL2 -LC:\mingw_dev_lib\lib -w -Wl,-subsystem,windows -lmingw32 -lSDL2main -lSDL2 -lSDL2_image -std=c++11
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?
目前这C:\Windows\System32是相当不切实际的。我知道您可以使用 更改当前目录:cd [directory],但是有什么方法可以更改新文件的默认保存位置,而不必一直更改目录?
也就是说,一个没有输入并且不返回任何内容的lambda.
我正在考虑在Python中模仿switch语句的聪明方法.这是我的尝试(无济于事):
statement = {
"Bob": lambda: print "Looking good, Bob!",
"Jane": lambda: print "Greetings, Jane!",
"Derek": lambda: print "How goes it, Derek?"
}[person]()
Run Code Online (Sandbox Code Playgroud) 我能想到的唯一原因是常规数组使用较少的内存(虽然它可能可以忽略不计)并且可以存储基元.即便如此,您也可以使用包装类.
我的理解是伪随机数生成器基本上只需要一些数字(种子),用一堆XOR和位移散列它,然后吐出一个非常长的数字,从中可以检索余数以获得"随机" "号码.
现在,通常你会time(NULL)在C/C++中用作rand()的种子.但是,time(NULL)每秒只增加一次,而不是每毫秒增加一次.那么,rand()如果种子仍然是相同的time(NULL)值,那么如何在不到一秒的时间内循环超过一千次并仍然可以得到不同的数字作为输出?
因此,当玩弄Python时,我注意到程序的堆栈大小基本上没有限制(在数字上继续执行电源操作,即使在达到数千个数字后,精度也保持完美).这让我想知道:如果我不小心进入Python的无限递归循环怎么办?编译器会注意到并抛出堆栈溢出错误吗?或者程序会崩溃吗?我的CPU会燃烧吗?老实说,这不是我渴望测试自己的东西.
例如,我正在尝试使用此递归解构器删除二叉树:
~BinTreeNode() {
delete left;
delete right;
// delete this; <- i'm assuming this is implicit, so i don't need to include it
}
Run Code Online (Sandbox Code Playgroud)
如果我确实delete root;root是根节点,那么整个树的内存是否会被成功释放?
所以,我正在制作一个随机的平假名发生器(不要问为什么,好吗?)并且我遇到了一些问题.随机名称生成器在大多数情况下工作正常但有时由于某种原因它会生成长串的重复辅音.因此,我没有像普通程序员那样直接解决问题,而是决定尝试扫描ArrayList并在随机生成后删除重复的字符:
ArrayList<String> name = new ArrayList<String>();
Iterator <String> it = name.iterator();
... // insert random generation here
for (h = 0; h < s; h++) { // s is the length of the ArrayList
...
String curInd = name.get(h);
String nextInd = name.get(h+1);
if (curInd.equals(nextInd)) { // NOT
name.remove(h); // WORKING
s--; // :(
}
}
String previousName = "";
while (it.hasNext()) {
String currentName = it.next();
if (currentName.equals(previousName)) {
it.remove();
}
previousName = currentName;
}
Run Code Online (Sandbox Code Playgroud)
这不起作用.我没有收到错误或其他任何内容,它只是不会删除重复的字符(或者更确切地说是重复的字符串,因为我将每个字符都设置为字符串.)可能是什么问题?
我有一个文本文件,其中包含不同的时间,如下所示:
12:30 am
4:50 PM
6:15 A.M.
8:09 p.m.
Run Code Online (Sandbox Code Playgroud)
等等
我想解析这个小时,分钟和子午线的文件.我尝试使用正则表达式"[: ]",但我一直IOException("Invalid meridian.")被我甩了...... 我做了一个例外.
这是一些代码:
try {
System.out.println("Enter the name of the input file: ");
inputFile = in.nextLine();
System.out.println("Enter the name of the output file: ");
outputFile = in.nextLine();
Scanner fileIn = new Scanner(new File(inputFile));
while (fileIn.hasNext()) {
String[] vals = fileIn.nextLine().split("[: ]");
int hours = Integer.parseInt(vals[0]);
int minutes = Integer.parseInt(vals[1]);
String meridian = vals[2];
times.add(new Time(hours, minutes, meridian));
}
fileIn.close();
System.out.println("Unsorted times: ");
for …Run Code Online (Sandbox Code Playgroud) 结构指针的成员变量返回错误的数字.
这是结构声明:
struct Obj {
int val;
Obj(int val) { val = val; }
};
Run Code Online (Sandbox Code Playgroud)
这是它变得时髦的地方:
Obj* cool = new Obj(4);
cout << cool->val; // outputs a number that's not 4
cout << (cool->val == 4); // prints 0... interesting
Run Code Online (Sandbox Code Playgroud) 我找不到有关如何在优先级队列中订购对象的任何信息.我试过这个:
class Person {
...
public:
bool operator<(const Person& p) {
return age < p.age;
}
}
int main() {
priority_queue<Person*> people;
people.push(new Person("YoungMan", 21));
people.push(new Person("Grandma", 83));
people.push(new Person("TimeTraveler", -5000));
people.push(new Person("Infant", 1));
while (!people.empty()) {
cout << people.top()->name;
delete people.top();
people.pop();
}
Run Code Online (Sandbox Code Playgroud)
并且它应该根据年龄给予优先级(老年人获得更高的优先级,因此将队列排在第一位),但它不起作用.但是我得到了这个输出:
Infant
Grandma
TimeTraveler
YoungMan
Run Code Online (Sandbox Code Playgroud)
我不知道这是什么命令,但它绝对不是年龄.