简体   繁体   English

C ++:确定变量是否不包含数据

[英]C++: Determining whether a variable contains no data

I've been messing around in C++ a little bit but I'm still pretty new. 我已经在C ++中弄乱了一点,但是我还是很新。 I searched around a little bit and even using the keywords of exactly the problem I am trying to tackle yields no results. 我进行了一些搜索,甚至使用的正是我要解决的问题的关键字都没有结果。 Basically I am just trying to figure out how to tell if a variable has no data. 基本上,我只是想弄清楚如何判断变量是否没有数据。 I have a file that my program reads and it searches for a specific character within that file and basically uses delimiters to determine where to store the actual data in a variable. 我有一个程序可以读取的文件,它会在该文件中搜索特定字符,并且基本上使用定界符来确定将实际数据存储在变量中的位置。 Now I added some comments in the file saying that it should not be edited which has caused me some problems. 现在,我在文件中添加了一些注释,指出不应对其进行编辑,这给我带来了一些问题。 So I pretty much want to count the number of comments, but I'm not sure how to do it because the way I had it set up was resulting in huge numbers being returned. 所以我非常想计算评论的数量,但是我不确定该怎么做,因为我设置它的方式导致返回了大量的评论。 So I figured I would attempt to fix it with a simple if statement to see if there was any data in the array while it was running the loop, and if there was then simply add +1 to my variable. 所以我想我会尝试使用一个简单的if语句来修复它,以查看数组在运行循环时是否存在任何数据,然后如果有的话只需将+1添加到我的变量中。 Needless to say it did not work. 不用说它没有用。 Here's the code. 这是代码。 And if you know a better way of doing this, by all means please do share. 如果您知道这样做的更好方法,请务必分享。

size_t arySearchData[20];
size_t commentLines[20];
size_t foundDelimiter;
size_t foundComment;
int commentsNum;

foundDelimiter = lineText.find("]");
foundComment = lineText.find("#");

if (foundComment != std::string::npos) {
    commentLines[20] = int(foundComment);

    if (foundComment = <PROBLEM>){
        commentsNum++;
    }
}

So it successfully gets the two comments in my file and recognizes that they are located at the first index(0) in each line but when I tried to have it just do commentsNum++ in my first if statement it just comes up with tons of random numbers, and I am not sure why. 因此,它成功获取了我文件中的两个注释,并识别出它们位于每行的第一个索引(0)处,但是当我尝试使其在我的第一个if语句中执行commentNum ++时,它只包含大量随机数,我不确定为什么。 So as I said my problem is within the second if statement, I need a void or just a better way to solve this. 因此,正如我所说的那样,我的问题在第二个if语句之内,我需要一种无效的方法或者只是一种更好的方法来解决此问题。 Any help would be greatly appreciated. 任何帮助将不胜感激。

And yes I do realize I could just determine if there 'was' data in the there rather than being void or null but then it would have to be specific and if the comment (#) had a space before it, then it would render my method of reading the file useless as the index will have changed. 是的,我确实知道我可以确定那里是否有数据,而不是确定是否为空或为空,但是它必须是特定的,并且如果注释(#)之前有空格,那么它将使我的读取文件无用的方法,因为索引将更改。

A variable in C++ always contains data, just it may not be initialised. C ++中的变量始终包含数据,只是它可能没有初始化。

int i;

It will have some value, what it is can't be determined until you do something like 它会有一些价值,直到您做类似的事情才能确定它的价值。

i = 1337;

until you do that the value of i will be what ever happened to be in the memory location that i has been assigned to. 直到您这样做, i的值才会成为i已分配给它的存储位置中的值。

The compile may pick up on the fact that you are trying to use a variable which you have not actually given a value your self, but this will normally just be a warning, as their is nothing wrong as such with doing so 编译可能会发现您正在尝试使用一个变量,而该变量实际上并没有给您自己赋值,但这通常只是一个警告,因为这样做没有错。

You do not initialize commentsNum. 您不初始化commentsNum。 Try this: 尝试这个:

int commentsNum = 0;

In C++ other than static variables, other variables are assigned undetermined values. C++static变量外,其他变量都分配有不确定的值。 This is primarily done to adhere to underlying philosophy -- "you don't pay for things you don't use", so it doesn't zero that memory by default." However, for static variables, memory is allocated at link time. Unlike runtime initialization, which would need to happen in local variables, link time allocation and initialization incur low cost. 这样做主要是为了遵守基本原理-“您不用为不使用的东西付费”,因此默认情况下不会将内存归零。”但是,对于static变量,内存是在链接时分配的与运行时初始化不同,运行时初始化需要在局部变量中进​​行,链接时间分配和初始化的成本较低。

I would recommend hence setting int commentsNum = 0; 因此,我建议设置int commentsNum = 0;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM