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;
}