always "using namespace std"?

This question already has an answer here: Why is “using namespace std” considered bad practice? 35 answers There is no "cstring type of string". The <cstring> header contains no string class, only functions (and the size_t type and the NULL macro). In your example, string would just be considered std::string . As for using namespace . I usually just use it in a very smal

总是“使用命名空间标准”?

这个问题在这里已经有了答案: 为什么“使用名称空间标准”被认为是不好的做法? 35个答案 没有“cstring类型的字符串”。 <cstring>头不包含字符串类,只包含函数(以及size_t类型和NULL宏)。 在你的例子中, string将被认为是std::string 。 至于using namespace 。 我通常只是在一个很小的范围内使用它,比如功能体内部。 永远不要在头文件中!

Declaring variables inside loops, good practice or bad practice?

Question #1: Is declaring a variable inside a loop a good practice or bad practice? I've read the other threads about whether or not there is a performance issue (most said no), and that you should always declare variables as close to where they are going to be used. What I'm wondering is whether or not this should be avoided or if it's actually preferred. Example: for(int count

在循环内声明变量,好的做法还是不好的做法?

问题1:在循环中声明一个变量是一种好的做法还是不好的做法? 我已经阅读了关于是否存在性能问题的其他线索(大多数是否定的),并且您应该始终将变量声明为接近它们将要使用的位置。 我想知道的是这是否应该避免,或者如果它真的是首选。 例: for(int counter = 0; counter <= 10; counter++) { string someString = "testing"; cout << someString; } 问题2:大多数编译器是否意识到变量已经被声明并

Why can templates only be implemented in the header file?

Quote from The C++ standard library: a tutorial and handbook: The only portable way of using templates at the moment is to implement them in header files by using inline functions. Why is this? (Clarification: header files are not the only portable solution. But they are the most convenient portable solution.) It is not necessary to put the implementation in the header file, see the alter

为什么只能在头文件中实现模板?

来自C ++标准库的引用:教程和手册: 目前使用模板的唯一便携方式是使用内联函数在头文件中实现它们。 为什么是这样? (澄清:头文件不是唯一的便携式解决方案,但它们是最便捷的便携式解决方案。) 没有必要将实现放在头文件中,请参阅本答案末尾的替代解决方案。 无论如何,你的代码失败的原因是,当实例化一个模板时,编译器用给定的模板参数创建一个新类。 例如: template<typename T> struct Foo {

std::wstring VS std::string

I am not able to understand the differences between std::string and std::wstring . I know wstring supports wide characters such as Unicode characters. I have got the following questions: When should I use std::wstring over std::string ? Can std::string hold the entire ASCII character set, including the special characters? Is std::wstring supported by all popular C++ compilers? What is e

std :: wstring VS std :: string

我无法理解std::string和std::wstring之间的区别。 我知道wstring支持宽字符,如Unicode字符。 我有以下问题: 什么时候应该使用std::wstring不是std::string ? std::string保存整个ASCII字符集,包括特殊字符吗? 所有流行的C ++编译器都支持std::wstring吗? 什么是“宽字符”? string ? wstring ? std::string是在char上模板化的basic_string ,在wchar_t上是std::wstring 。 char与wchar_t char应该包

using std::<type> v.s. using std namespace

This question already has an answer here: Why is “using namespace std” considered bad practice? 35 answers It depends. If you want to inject a single name into another scope, the using-declaration is better, eg namespace foolib { // allow vector to be used unqualified within foo, // or used as foo::vector using std::vector; vector<int> vec(); template<typename T> s

使用std :: <type> vs使用std命名空间

这个问题在这里已经有了答案: 为什么“使用名称空间标准”被认为是不好的做法? 35个答案 这取决于。 如果你想注入一个单一的名称到另一个范围,使用声明更好,例如 namespace foolib { // allow vector to be used unqualified within foo, // or used as foo::vector using std::vector; vector<int> vec(); template<typename T> struct Bar { T t; }; template<typename T> void s

Does C++11 change the behavior of explicitly calling std::swap to ensure ADL

Background Consider for this question the following code: #include <utility> namespace ns { struct foo { foo() : i(0) {} int i; private: foo(const foo&); // not defined, foo& operator=(const foo&); // non-copyable }; void swap(foo& lhs, foo& rhs) { std::swap(lhs.i, rhs.i); } } template <typenam

C ++ 11是否会更改显式调用std :: swap的行为以确保ADL

背景 考虑这个问题下面的代码: #include <utility> namespace ns { struct foo { foo() : i(0) {} int i; private: foo(const foo&); // not defined, foo& operator=(const foo&); // non-copyable }; void swap(foo& lhs, foo& rhs) { std::swap(lhs.i, rhs.i); } } template <typename T> void do_swap(T&

Confusion about pointers and references in C++

I have a bunch of code like this: #include <iostream> using namespace std; void swap(int *a, int *b) { int temp = *a; *a = *b; *b = temp; } int main() { int a; int b; a = 7; b = 5; swap(a, b); cout << a << b; return 0; } This code does the swapping process as what I exactly wanted to swap 2 numbers But when I want two numbers from

关于C ++中指针和引用的混淆

我有一堆这样的代码: #include <iostream> using namespace std; void swap(int *a, int *b) { int temp = *a; *a = *b; *b = temp; } int main() { int a; int b; a = 7; b = 5; swap(a, b); cout << a << b; return 0; } 这段代码完成交换过程,因为我确实想要交换2个数字 但是,当我需要用户的两个号码如下: int a; int b; cin >> a; cin >>

c++

Referring the SO C++ FAQ When should static_cast, dynamic_cast and reinterpret_cast be used?. const_cast is used to remove or add const to a variable and its the only reliable, defined and legal way to remove the constness. reinterpret_cast is used to change the interpretation of a type. I understand in a reasonable way, why a const variable should be casted to non-const only using const_cas

C ++

引用SO C ++常见问题什么时候应该使用static_cast,dynamic_cast和reinterpret_cast ?. const_cast用于删除或添加const到一个变量,它是唯一可靠的,定义的和合法的方法来消除常量。 reinterpret_cast用于更改类型的解释。 我理解一个合理的方式,为什么一个const变量只能使用const_cast转换为非const,但我无法找到使用reinterpret_cast代替const_cast来添加const的合理理由。 我明白,使用reinterpret_cast甚至添加常量

What is the difference between static

Possible Duplicate: When should static_cast, dynamic_cast and reinterpret_cast be used? I'm using c function in c++, where a structure passed as a void type argument in c is directly stored that same structure type. eg in C. void getdata(void *data){ Testitem *ti=data;//Testitem is of struct type. } to do the same in c++ i use static_cast: void foo::getdata(void *data){ Testi

静态有什么区别

可能重复: 什么时候应该使用static_cast,dynamic_cast和reinterpret_cast? 我在c ++中使用c函数,其中在c中作为void类型参数传递的结构直接存储在相同的结构类型中。 例如在C. void getdata(void *data){ Testitem *ti=data;//Testitem is of struct type. } 在c ++中做同样的事情我使用static_cast: void foo::getdata(void *data){ Testitem *ti = static_cast<Testitem*>(data); } 当我使用reint

What should main() return in C and C++?

What is the correct (most efficient) way to define the main() function in C and C++ — int main() or void main() — and why? If int main() then return 1 or return 0 ? There are numerous duplicates of this question, including: What are the valid signatures for C's main() function? The return type of main() function Difference between void main() and int main() ? main() 's signatur

main()应该在C和C ++中返回什么?

什么是在C和C ++中定义main()函数的正确(最有效的)方法 - int main()或void main() - 为什么? 如果int main()则return 1或return 0 ? 这个问题有很多重复,包括: C的main()函数的有效签名是什么? main()函数的返回类型 void main()和int main()之间的区别? main()在C ++中的签名 什么是main()的正确声明? - 对于C ++,确实有非常好的答案。 C中main()函数的样式 C中main()方法的返回类型 int main(