Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'std::string' has no member named 'front'

Tags:

c++

string

#include <string>
#include <algorithm>
#include <iostream>

int main()
{
    string str;
    string str1;
    int h = 0;
    cin >> str;
    if (str.length() > 10)
    {
        str1 += str.front();
        h = str.length() - 2;
        string s = to_string(h);
        str1 += s;
        str1 += str.back();
        cout << str1;
    }
    else cout << str;
    return 0;
}

compiles in XCode, but doesn`t on codeforces.ru/

 сan't compile program.cpp:
program.cpp: In function 'int main()':
program.cpp:23:21: error: 'std::string' has no member named 'front'
program.cpp:27:29: error: 'to_string' was not declared in this scope
program.cpp:32:21: error: 'std::string' has no member named 'back'
like image 681
driver733 Avatar asked May 31 '13 16:05

driver733


People also ask

How do you check if a string starts with a letter in C++?

Starting with C++20, you can use the starts_with method. std::string s = "abcd"; if (s. starts_with("abc")) { ... }

What is std :: string& in C++?

std::string class in C++ C++ has in its definition a way to represent a sequence of characters as an object of the class. This class is called std:: string. String class stores the characters as a sequence of bytes with the functionality of allowing access to the single-byte character.

What is the difference between a C++ string and an std::string?

There is no functionality difference between string and std::string because they're the same type.

Why string is not declared in this scope C++?

string is in the std namespace. You have the following options: Write using namespace std; after the include and enable all the std names: then you can write only string on your program. Write using std::string after the include to enable std::string : then you can write only string on your program.


1 Answers

One thing is that string::front and std::to_string are introduced since C++11. You have to make sure that you are using a compiler that supports those new features.

like image 115
taocp Avatar answered Sep 29 '22 08:09

taocp