std::string my_string = "";
char test = my_string[0];
Run Code Online (Sandbox Code Playgroud)
我注意到这不会崩溃,每次我测试它,测试都是0.
我可以依赖它总是0吗?还是随意的?
这是不好的编程吗?
编辑:从一些评论中,我认为对此的有用性存在一些误解.
这样做的目的不是检查字符串是否为空.不需要检查字符串是否为空.
情况是有一个字符串可能是空的也可能不是.我只关心这个字符串的第一个字符(如果它不是空的).
在我看来,检查字符串是否为空是不太有效,然后,如果它不是空的,请查看第一个字符.
if (! my_string.empty())
test = my_string[0];
else
test = 0;
Run Code Online (Sandbox Code Playgroud)
相反,我只需查看第一个字符,无需检查字符串是否为空.
test = my_string[0];
Run Code Online (Sandbox Code Playgroud) 我遇到了其他人写的这个小型JavaScript程序(在可汗学院):
/*vars*/
frameRate(0);
var Sz=100;
var particles=1000;
scale(400/Sz);
var points=[[floor(Sz/2),floor(Sz/2),false]];
for(var i=0;i<particles;i++){
points.push([floor(random(0,Sz)),floor(random(0,Sz)),true]);
}
var l=points.length-1;
var dirs=[[0,1],[1,0],[0,-1],[-1,0]];
/*functions*/
var move=function(p1){
var mv=dirs[floor(random(0,4))];
var temp=true;
for(var i=l;i>=0;i--){
if(!points[i][2]&&points[i][0]===p1[0]+mv[0]&&points[i][1]===p1[1]+mv[1]){
temp=false;
p1[2]=false;
i=0;
}
}
if(temp){
p1[0]+=mv[0];
p1[1]+=mv[1];
if(p1[0]<0){p1[0]=0;}
if(p1[0]>Sz){p1[0]=Sz;}
if(p1[1]<0){p1[1]=0;}
if(p1[1]>Sz){p1[1]=Sz;}
}
};
/*draw*/
draw= function() {
background(255);
for(var i=points.length-1;i>=0;i--){
stroke(0);
if(points[i][2]){
move(points[i]);
}
else{
stroke(0,0,255);
}
point(points[i][0],points[i][1]);
}
};
Run Code Online (Sandbox Code Playgroud)
我查看了代码并发现它有点难以阅读.所以我决定用一些面向对象制作我自己的版本:
// apparently, object orientation is a lot slower than just putting the data in …Run Code Online (Sandbox Code Playgroud) 我正在尝试创建一个类的全局实例,其构造函数引用一个全局变量.
该程序编译没有错误.但是当它运行时,它会在全局变量的引用上崩溃.
如何在没有构造函数崩溃的情况下创建此类的全局实例?
这是我制作的SSCCE:
/* main.cpp */
#include "TestClass.h"
// I need a global instance of TestClass
TestClass j;
int main()
{
return 0;
}
Run Code Online (Sandbox Code Playgroud)
-
/* C.h */
#ifndef C_H_INCLUDED
#define C_H_INCLUDED
#include <string>
// global
extern const std::string S;
#endif // C_H_INCLUDED
Run Code Online (Sandbox Code Playgroud)
-
/* C.cpp */
#include "C.h"
#include <string>
// extern definition of global
const std::string S = "global string data";
Run Code Online (Sandbox Code Playgroud)
-
/* TestClass.h */
#ifndef TESTCLASS_H_INCLUDED
#define TESTCLASS_H_INCLUDED
class TestClass
{
public:
TestClass();
};
#endif // …Run Code Online (Sandbox Code Playgroud) 这适用于Visual Studio,它适用于一台计算机上的GCC 4.9.2.
但在另一台计算机上,我认为它是相同的GCC 4.9.2编译器,但它给了我这个错误.
我错过了什么吗?这是怎么回事?
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
string filename;
filename = "teststring";
ofstream fout;
fout.open(filename);
fout << "Hello world!" << endl;
fout.close();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
.
||=== Build: Debug in fileiotest (compiler: TDM32 GNU GCC Compiler 4.9.2 dw2) ===|
F:\Users\XXX\cpp\fileiotest\main.cpp||In function 'int main()':|
F:\Users\XXX\cpp\fileiotest\main.cpp|12|error: no matching function for call to 'std::basic_ofstream<char>::open(std::string&)'|
F:\Users\XXX\cpp\fileiotest\main.cpp|12|note: candidate is:|
F:\TDM-GCC-32\lib\gcc\mingw32\4.9.2-dw2\include\c++\fstream|716|note: void std::basic_ofstream<_CharT, _Traits>::open(const char*, std::ios_base::openmode) [with _CharT = char; _Traits = std::char_traits<char>; std::ios_base::openmode = …Run Code Online (Sandbox Code Playgroud)