0
#include <iostream>
#include <string>
#include <map>
using namespace std;

int main()
{
    map<string, string> opposites;
    opposites["A"] = "T";
    opposites["T"] = "A";
    opposites["C"] = "G";
    opposites["G"] = "C";
    string line = "AGATTATATAATGATAGGATTTAGATTGACCCGTCATGCAAGTCCATGCATGACAGC";
    int promoter_index,i;
    for (i = 0; i < line.length();i++)
    {
        if (line[i] == "T" && line[i+1] == "A" && line[i+2] == "T" && line[i+3] == "A" && line[i+4] == "A" && line[i+5] == "T")
            promoter_index = i;
    }
    int start_of_sequence = promoter_index + 10;
    cout << promoter_index;

}

我想在“行”中找到 TATAAT,但是当我尝试比较它时说“ISO C++ 禁止指针和整数之间的比较”。为什么?

4

4 回答 4

4

std::string::operator[] returns a character, not a c-string.

if (line[i] == "T" && line[i+1] == "A"...

should be

if (line[i] == 'T' && line[i+1] == 'A'...

The single-quote denotes a character, whereas the double-quote denotes a c-string.

c-strings are of type const char[], which is implicitly converted to a const char*. chars are integral types in c++, so what the compiler is telling you is that you can't compare a pointer (the c-string "T") to an integral type (the char returned by the operator[]). The reason that the you're being told that you can't do that comparison in ISO C++ is that in the dark, brutal days of pre-standardization C++, you could do things like compare integral types and pointers for equality.

于 2013-07-26T13:37:42.607 回答
4

This is because std::string::operator[] returns a reference to a char, whereas a string literal such as "T" is a const char[N], which decays to const char* in the comparison. If you want to compare an element of line to a single character, use single quotes:

if (line[i] == 'T' && line[i+1] == 'A' ....
于 2013-07-26T13:37:57.573 回答
3

You are erroneously attempting to compare individual characters to entire strings.

Using the subscript ([]) operator on a string gives you a single character, but "T" is a string literal. (Yes, even though it only has one character.)

line[i] == "T"
/*****/   /***/
//  ^       ^
//  |       |
// char     |
//     char const[2]

Character literals are delimited by single-quotes, so you meant to write:

line[i] == 'T'

So, in context:

if (line[i]   == 'T'
 && line[i+1] == 'A'
 && line[i+2] == 'T'
 && line[i+3] == 'A'
 && line[i+4] == 'A'
 && line[i+5] == 'T')

Actually, it also has a second character which you can't see — the null terminator.

于 2013-07-26T13:36:45.563 回答
0

要让您的程序编译,只需将文字更改为''. 例如line[i] == 'T'

或者,您可以使用strcmp(&line[i], "T") != 1, 比较两个字符的值是否相等。

于 2013-07-26T13:46:35.440 回答