string is in std namespace, it's only valid to use std::string rather than string (the same for std::cin, std::vector etc). However, in practice, some compilers may let a program using string etc without std:: prefix compile, which makes some programmers think it's OK to omit std::, but it's not in standard C++.
So it's best to use:
#include <iostream>
#include <string>
int main()
{
std::string a;
std::cin >> a;
std::cout << a << std::endl;
return 0;
}
Note that it's NOT a good idea to use using namespace std; (though it's legal), especially NOT put it in a header.
If you are tired of typing all the std::, declare all the names with namespace you use is one option:
#include <iostream>
#include <string>
using std::string;
using std::cin;
using std::cout;
using std::endl;
int main()
{
string a;
cin >> a;
cout << a << endl;
return 0;
}
using namespace stdusing namespace stdbefore the use of string will allow you to use the std::string class without the namespace prefix.using namespace std;just after the includes - although you might not want to pull everything in std into your program.using std::string;is a safer idea thanusing namespace std;. The latter gets you hundreds of things, some of which might have unexpected side effects.