简体   繁体   English

C ++字符串和char的比较

[英]C++ Comparison of a string and a char

I am trying to compare a string and a char like below : 我想比较一个字符串和一个像下面的字符:

char filter = "    " // <<== this is a tab space, so I am assuming its one char
if ( line[counter] == filter[0]){

}

note that line is a normal string defined like : string line; 请注意,该行是一个正常的字符串,定义如下: string line; . now for some reason the statement is never true even though there are no syntax errors. 由于某种原因,即使没有语法错误,该语句也永远不会成立。

UPDATE UPDATE

inside line is : string line = "1 90 74 84 48 76 76 80 85"; 内线是: string line = "1 90 74 84 48 76 76 80 85";

2nd UPDATE 第二次更新

here is the complete function I wrote : 这是我写的完整函数:

void getResults(string line){

    int tmpSize = line.length();
    int counter = 0;
    int tmpCounter = 0;

    while(counter != MAX_No_Of_Grades){

        if(line[counter] == '\t\t'){
            counter++;
        }else{
            cout << tmpCounter << ". this is : " << line[tmpCounter] << "a" << endl;
            tempGrade[counter] += line[tmpCounter];
        }
        tmpCounter++;
    }


}

the function is technically suppose to break the string line into an array by "tabspace". 从技术上讲,该函数假设通过“tabspace”将字符串line分解为数组。 but right now counter does not change and therefore its an endless loop! 但是现在counter不会改变,因此它是一个无限循环!

I'm not sure what code you are actually running, because the char filter = " " line has no terminator and that assignment is definitely illegal. 我不确定你实际运行的代码是什么,因为char filter = " "行没有终止符,并且该赋值绝对是非法的。 You cannot assign the string literal (a char array) to a single char variable. 您不能将字符串文字( char数组)分配给单个char变量。

If you want to determine whether the nth character of a string is a tabulation, the following code would probably be what you are looking for: 如果要确定字符串的第n个字符是否为制表符,则以下代码可能就是您要查找的内容:

if (line[counter] == '\t') {
  // match...
}

As for updates. 至于更新。 If typed as string line = "1 90 74 84 48 76 76 80 85"; 如果输入为string line = "1 90 74 84 48 76 76 80 85"; in your program, there are no tabulations in this string. 在您的程序中,此字符串中没有列表。 There are only spaces. 只有空间。 Moreover, '\\t\\t' is a pair of tables. 而且, '\\t\\t'是一对表格。 Put a single \\t in there for a single tab. 在单个选项卡中放置一个\\t

Here is a slightly modified version of your sample function: 以下是您的示例函数的略微修改版本:

void getResults(string line)
{
    int tmpSize = line.length();
    int counter = 0;
    int tmpCounter = 0;
    while((tmpCounter < tmpSize) && (counter != MAX_No_Of_Grades))
    {
        if(line[counter] == ' ') {
            counter++;
        }
        else {
            cout << tmpCounter << ". this is : " << line[tmpCounter] << "a" << endl;
            tempGrade[counter] += line[tmpCounter];
        }
        tmpCounter++;
    }
}

This version has an extra check in the while loop to stop consuming characters after the end of line is reached. 此版本在while循环中有一个额外的检查,以便在到达line尾后停止使用字符。 It also uses a space character since your test input does not use tabulations. 它还使用空格字符,因为您的测试输入不使用制表符。


If this is not homework and you can use all the standard library facilities you want, I would suggest looking into more advanced input strategies. 如果这不是功课,你可以使用你想要的所有标准库设施,我建议你研究更高级的输入策略。 Here is a simplified version of your function to extract every single number in the array. 以下是函数的简化版本,用于提取数组中的每个数字。

#include <iostream>
#include <string>
#include <sstream>
void getResults ( const std::string& line )
{
    std::istringstream input(line);
    for (int grade=0; input >> grade;) {
        // process grade.
    }
}

If you want to remove the global variable and handle any number of grades, you can use a std::vector<> to automatically increase the "array" size as you get more and more grades. 如果要删除全局变量并处理任意数量的等级,可以使用std::vector<>在获得越来越多的等级时自动增加“数组”大小。

#include <iostream>
#include <string>
#include <sstream>
#include <vector>
std::vector<int> getResults ( const std::string& line )
{
    std::istringstream input(line);
    std::vector<int> grades;
    for (int grade=0; input >> grade;) {
        grades.push_back(grade);
    }
    return grades;
}
char filter = "    " // <<== this is a tab space, so I am assuming its one char

What you are assuming is wrong. 你所假设的是错的。 A tab space char is '\\t' . 制表符空格char'\\t'

What you have there instead is multiple space characters. 你所拥有的是多个空格字符。

Edit: 编辑:

Also this : char filter = " " is wrong 另外这个: char filter = " "是错误的

This const char * filter = " "; 这个const char * filter = " "; //is more like it... //更像是......

But this : char = '\\t'; 但是这个: char = '\\t'; is maybe what you wanted. 也许是你想要的。

Ok. 好。 Lets go over you code by parts to make things clear. 让我们逐个查看代码,以便清楚地说明问题。

char filter = "    " // <<== this is a tab space, so I am assuming its one char

Your first statement creates a variable of type char. 您的第一个语句创建一个char类型的变量。 The type char in c++ take a value, like any other variable. 与其他任何变量一样,c ++中的char类型采用值。 The only difference is that if you try to use it with a statement, like cout, it will be interpreted as a character by it's ASCII value. 唯一的区别是,如果你尝试将它与cout这样的语句一起使用,它将被它的ASCII值解释为一个字符。 What you did compiles and works, but the char you declared has the value that c++ gives for the string " ". 你做了什么编译和工作,但你声明的char具有c ++给字符串“”的值。 The value for the horizontal tab (TAB) in ASCII is 9. ASCII中水平制表符(TAB)的值为9。

So, for declaring a char of datatype char with the value for a TAB: 因此,为了使用TAB的值声明数据类型为char的char:

char filter = 9;

But you don't have to keep remembering all those values. 但是你不必记住所有这些值。 The same way C++ will give a value for the string " ", it will give a value for a character. 与C ++为字符串“”赋值的方式相同,它将为字符赋值。 If you want to get the value for characters, you have to use ''. 如果你想获得字符的值,你必须使用''。 A confusion that some make is to assume that tab is actually a group of spaces. 有人提出的混淆是假设tab实际上是一组空格。 Tabs are interpreted, by a text editor, as a group of spaces. 选项卡由文本编辑器解释为一组空格。 But it is in fact a single character. 但它实际上是一个单一的角色。 In C++, the character for the tab is \\t. 在C ++中,选项卡的字符是\\ t。 If you actually put this inside a string and call cout, you will have a tab. 如果你实际把它放在一个字符串中并调用cout,你将有一个标签。 The reason why \\t is a single character and not two characters is because the compiler gives "special" interpretations for the immediate character after a backslash inside '' or "". \\ t是单个字符而不是两个字符的原因是因为编译器在''或“”内的反斜杠后对直接字符给出“特殊”解释。

So the following will give the value of 9 to a variable of datatype char: 因此,以下内容将9的值赋给数据类型为char的变量:

char filter = '\t';

Now for your if statement: 现在为你的if语句:

if ( line[counter] == filter[0])

Your if statement is comparing the character with index "counter" in the string line. 您的if语句将字符与字符串行中的索引“counter”进行比较。 This is possible because a string is an array of characters (It is actually a class, but don't worry about it for now). 这是可能的,因为字符串是一个字符数组(它实际上是一个类,但现在不用担心它)。 But filter is not an array, it is a single character. 但是filter不是一个数组,它只是一个字符。 So it wont have an index (Eg [0]). 所以它没有索引(例如[0])。 All you need is the variable you created. 您所需要的只是您创建的变量。 This confusion usually happens because the concept of string is often introduced without explaining that a string is not a datatype (That's why is not blue, and that's why you have to include a library). 这种混淆通常是因为字符串的概念经常被引入而没有解释字符串不是数据类型(这就是为什么不是蓝色,这就是为什么你必须包含一个库)。

So, just removing the index you will have: 所以,只需删除您将拥有的索引:

if (line[counter] == filter)

You could, of course, just compare it to a tab directly 当然,您可以直接将它与选项卡进行比较

if (line[counter] == 9);

or 要么

if (line[counter] == '\t');

Hope this gives you a better understanding of what is actually happening. 希望这能让您更好地了解实际发生的情况。

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

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