First character of string c ++ [closed]

0

Good, I would like to know when the first character of a string is "#" and I do not know what function to use. Can somebody help me?

    
asked by nerviosus 18.12.2017 в 23:29
source

1 answer

6

A string of characters is nothing more than an array of characters (the class std::string encapsulates this same complexity). So, and taking into account that the indexes in C ++ start at 0, you have a wide range of possibilities to choose from:

std::string cadena = "ABCDEF";
std::cout << cadena[0]            // (1)
          << cadena.at(0)         // (2)
          << cadena.front()       // (3) 
          << *cadena.begin()      // (4)
          << *std::begin(cadena); // (5)
  • Index operator
  • at is similar to the index operator. Check that the index is valid and if it does not throw an exception
  • Direct access to the first character of the string
  • Using iterators
  • Another way to use iterators
  • So to know if the first character is '#' it is enough to apply the comparison operator to one of the options we have just seen:

    if( cadena.front() == '#' )
    {
      // ...
    }
    
        
    answered by 18.12.2017 / 23:39
    source