C++ 打字是不相关的——你的编辑应该替你打字。这很好,因为它使代码比到处都有std::更具可读性,并且比使用std::whatever的三十行更易于维护在每个文件的顶部。我可以在一行中包含来自std的多个元素吗?例如:使用std::string,std::mak
C++ 打字是不相关的——你的编辑应该替你打字。这很好,因为它使代码比到处都有std::更具可读性,并且比使用std::whatever的三十行更易于维护在每个文件的顶部。我可以在一行中包含来自std的多个元素吗?例如:使用std::string,std::mak,c++,namespaces,include,using,using-directives,C++,Namespaces,Include,Using,Using Directives,打字是不相关的——你的编辑应该替你打字。这很好,因为它使代码比到处都有std::更具可读性,并且比使用std::whatever的三十行更易于维护在每个文件的顶部。我可以在一行中包含来自std的多个元素吗?例如:使用std::string,std::make_shared; using namespace std; #include <iostream> using namespace std; ... cout << "Hi" << endl; #in
打字是不相关的——你的编辑应该替你打字。这很好,因为它使代码比到处都有
std::
更具可读性,并且比使用std::whatever的三十行更易于维护代码>在每个文件的顶部。我可以在一行中包含来自std的多个元素吗?例如:使用std::string,std::make_shared;
using namespace std;
#include <iostream>
using namespace std;
...
cout << "Hi" << endl;
#include <iostream>
...
std::cout << "Hi" << std::endl;
std::cout << "I am accessing stdout" << std::endl;
using std::cout;
using std::endl;
cout << "Hello" << endl;
std::cout << "Hello" << std::endl;
using namespace std;
using std::string;
using namespace std;
std::cout << "cout is declared within the namespace std";
using namespace std;
string myString;
std::string myString;
using std::string;
string myString;
using std::cout;
using namespace std;
namespace intutils
{
int addNumbers(int a, int b)
{
return a + b;
}
}
#include "common1.h"
int main()
{
int five = 0;
five = addNumbers(2, 3); // Will fail to compile since the function is in a different namespace.
five = intutils::addNumbers(2, 3); // Will compile since you have made explicit which namespace the function is contained within.
using namespace intutils;
five = addNumbers(2, 3); // Will compile because the previous line tells the compiler that if in doubt it should check the "intutils" namespace.
}
#include <iostream>
#include <algorithm>
namespace zzz
{
struct X {};
void swap(zzz::X&, zzz::X&)
{
std::cout << "Swapping X\n";
}
}
template <class T>
void dumb_swap(T& a, T& b)
{
std::cout << "dumb_swap\n";
std::swap(a, b);
}
template <class T>
void smart_swap(T& a, T& b)
{
std::cout << "smart_swap\n";
using std::swap;
swap(a, b);
}
int main()
{
zzz::X a, b;
dumb_swap(a, b);
smart_swap(a, b);
int i, j;
dumb_swap(i, j);
smart_swap(i, j);
}
//copy numbers larger than 1 from stdin to stdout
remove_copy_if(
std::istream_iterator<int>(std::cin), std::istream_iterator<int>(),
std::ostream_iterator<int>(std::cout, "\n"),
std::bind2nd(std::less_equal<int>(), 0)
);
// cstdio
namespace std
{
// ...
int printf(const char* ...);
// ...
}
// stdio.h
#include <cstdio>
using namespace std;