我已经告诉别人,编写using namespace std;代码是错误的,我应该用std::cout和std::cin直接代替.
为什么被using namespace std;认为是不好的做法?是低效还是冒着声明模糊变量(与名称std空间中的函数具有相同名称的变量)的风险?它会影响性能吗?
关于std命名空间使用'using'似乎有不同的看法.
有人说使用' using namespace std',其他人说不要,而是先加上与' std::' 一起使用的std函数,而其他人则说使用这样的东西:
using std::string;
using std::cout;
using std::cin;
using std::endl;
using std::vector;
Run Code Online (Sandbox Code Playgroud)
对于要使用的所有std函数.
各自的优点和缺点是什么?
我正在计算两点之间的距离.我用C++中的向量存储的两点:(0,0)和(1,1).
我应该得到结果
0
1.4
1.4
0
Run Code Online (Sandbox Code Playgroud)
但我得到的实际结果是
0
1
-1
0
Run Code Online (Sandbox Code Playgroud)
我认为在向量中使用迭代器的方式有问题.我该如何解决这个问题?
我发布了以下代码.
typedef struct point {
float x;
float y;
} point;
float distance(point *p1, point *p2)
{
return sqrt((p1->x - p2->x)*(p1->x - p2->x) +
(p1->y - p2->y)*(p1->y - p2->y));
}
int main()
{
vector <point> po;
point p1; p1.x = 0; p1.y = 0;
point p2; p2.x = 1; p2.y = 1;
po.push_back(p1);
po.push_back(p2);
vector <point>::iterator ii;
vector <point>::iterator jj;
for (ii = po.begin(); ii != po.end(); ii++) …Run Code Online (Sandbox Code Playgroud) 我格式化了如下数据:
Words 5 AnotherWord 4 SomeWord 6
它在一个文本文件中,我使用ifstream来读取它,但是如何将数字和单词分开?这个单词只包含字母,单词和数字之间会有一些空格或标签,不确定多少.
如果我在log某处定义名称空间并使其在全局范围内可访问,则会与double log(double)标准cmath标题冲突.实际上,大多数编译器似乎也同意它 - 大多数版本的SunCC,MSVC,GCC - 但GCC 4.1.2没有.
不幸的是,似乎没有办法解决歧义,因为using声明对于命名空间标识符是不合法的.你知道我可以log::Log在全局命名空间中编写任何方式cmath吗?
谢谢.
编辑:有人会知道C++ 03标准对此有何看法?我原以为范围运算符足以消除log下面代码示例中的使用歧义.
#include <cmath>
namespace foo
{
namespace log
{
struct Log { };
} // namespace log
} // namespace foo
using namespace foo;
int main()
{
log::Log x;
return 0;
}
// g++ (GCC) 4.1.2 20070115 (SUSE Linux)
// log.cpp: In function `int main()':
// log.cpp:20: error: reference to `log' is ambiguous
// /usr/include/bits/mathcalls.h:110: …Run Code Online (Sandbox Code Playgroud) 如果我#include <vector.h>输入我的源文件,我会收到此警告:
make -f Makefile CFG=Debug
g++ -c -g -o "Debug/mynn.o" "mynn.cpp"
In file included from C:/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/c++/3.4.5/backward/vector.h:59,
from mynn.h:7,
from mynn.cpp:1:
**C:/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/c++/3.4.5/backward/backward_warning.h:32:2: warning: #warning This file includes at least one deprecated or antiquated header. Please consider using one of the 32 headers found in section 17.4.1.2 of the C++ standard. Examples include substituting the <X> header for the <X.h> header for C++ includes, or <iostream> instead of the deprecated header <iostream.h>. To disable this warning use -Wno-deprecated.**
g++ …Run Code Online (Sandbox Code Playgroud) 我一直在阅读C++ For Everyone这本书,其中一个练习据说要编写一个string reverse(string str)返回值相反的函数str.
有人可以写一些基本代码并向我解释一下吗?从昨天开始我就一直盯着这个问题而无法理解.我得到的最远的是函数返回的第一个字母str(我还不知道它是怎么回事)
这是我得到的(发布此问题后一小时):
string reverse(string str)
{
string word = "";
if (str.length() <= 1)
{
return str;
}
else
{
string str_copy = str;
int n = str_copy.length() - 1;
string last_letter = str_copy.substr(n, 1);
str_copy = str_copy.substr(0, n);
word += reverse(str_copy);
return str_copy;
}
return word;
}
Run Code Online (Sandbox Code Playgroud)
如果我输入"狼",它将返回Wol.有人帮助我在这里如果我return word而不是return str_copy那时我得到一个w
如果我return last_letter然后我得到一个l
我知道这已被问过很多,但我能找到的唯一答案就是当const-ness实际上是使用(int*)或类似的方法进行的.当没有涉及强制转换时,为什么const限定符不在const对象上处理指针类型成员变量?
#include <iostream>
class bar {
public:
void doit() { std::cout << " bar::doit() non-const\n"; }
void doit() const { std::cout << " bar::doit() const\n"; }
};
class foo {
bar* mybar1;
bar mybar2;
public:
foo() : mybar1(new bar) {}
void doit() const {
std::cout << "foo::doit() const\n";
std::cout << " calling mybar1->doit()\n";
mybar1->doit(); // This calls bar::doit() instead of bar::doit() const
std::cout << " calling mybar2.doit()\n";
mybar2.doit(); // This calls bar::doit() const correctly
}
// ... (proper copying elided …Run Code Online (Sandbox Code Playgroud) 我有一个任意的STL容器C,它包含任意类型T的元素.我想创建一个std :: vector,它包含所有元素的副本.最干净的方法是什么?
template <typename C>
void myfunction(C container){
/*Derive the type T of elements within the container*/
std::vector<T> mystack;
/* Iterate over container and push_back() the elements into mystack*/
}
Run Code Online (Sandbox Code Playgroud) 可能重复:
如何将此代码转换为使用字符串
我有这样的功能:
char *foo()
{
}
Run Code Online (Sandbox Code Playgroud)
我怎样才能让它返回一个字符串呢?我试过了
string foo()
{
}
Run Code Online (Sandbox Code Playgroud)
但是编译器抱怨道.