#include <iostream>
#include <string>
using namespace std;
int main()
{
int a = 5;
int& b = a;
int* c = &a;
cout << "CASE 1" << endl;
cout << "a is " << a << endl << "b is " << b << endl << "c is " << *c << endl;
b = 10;
cout << endl << "a is " << a << endl << "b is " << b << endl << "c is " << …Run Code Online (Sandbox Code Playgroud) 我有一个函数,true如果元素从地图中删除,则返回false.以下是我的函数定义:
template <class Key, class Value>
bool HashMap<Key, Value>::remove(Key k)
{
int result = map.erase(k);
return (result == 1);
}
Run Code Online (Sandbox Code Playgroud)
当我试图检查它是否有效时,我发现了非常奇怪的行为.
当我尝试使用以下语法打印结果时:
cout << boolalpha << students.remove("Sam") << " | " << students.remove("Sam") endl;
Run Code Online (Sandbox Code Playgroud)
这种印刷false | true应该是true | false按我的知识.然后我尝试使用另一种方法打印它:
bool b1 = students.remove("Sam");
bool b2 = students.remove("Sam");
cout << boolalpha << b1 << " | " << b2 << endl;
Run Code Online (Sandbox Code Playgroud)
这打印出了预期的结果 - > true | false.我想这是编译器优化代码的一个技巧.但猜测总是不对的吗?(我正在使用g++ 4.8.5编译器)
谁能告诉我这里发生了什么?
我尝试运行一个简单的 C++ 程序,但我不确定为什么不同的编译器提供不同的输出
#include <iostream>
struct A
{
A() { std::cout << "Object A created" << std::endl; }
friend std::ostream& operator<<(std::ostream& os, const A& a)
{
os << a.str << "\n";
return os;
}
static bool printMe()
{
std::cout << "static bool printMe() of object A" << std::endl;
return true;
}
std::string str{"This is object A"};
};
int main()
{
std::cout << A() << (A::printMe() ? "TRUE" : "FALSE") << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
OUTPUT1:一些编译器提供以下输出:
Object A created
This is object A …Run Code Online (Sandbox Code Playgroud) 比方说,我有一个很长的声明
cout << findCurrent() << "," << findLowest() << "," << findHighest() << "," << findThird()<<"\n";
会像逻辑指令一样findCurrent()运行findLowest()吗?
pop()函数有什么问题,为什么它不能正常工作?
class stack{
int *p, *Cursor;
int size ;
public:
stack(int sz) {Cursor = p = new int[size=sz+1];} //consider the stack empty when its size is 1
~stack() {delete[] p;} //Cursor and P will be destroyed when the program finishes
void push(int x) {Cursor+=1; *Cursor=x; size++;}
int pop() {if(Cursor == p) return -1; int temp = *Cursor; Cursor--; size--; return (temp);}
bool isEmpty(){return(Cursor == p);}
bool isFull(){return(Cursor == p+size);}
};
Run Code Online (Sandbox Code Playgroud)
这是我的测试:
stack A(3);
std::cout<<"Empty: "<<A.isEmpty()<<std::endl;
std::cout<<"Full: "<<A.isFull()<<std::endl;
A.push(10);
A.push(20);
A.push(30); …Run Code Online (Sandbox Code Playgroud) 可能重复:
使用std :: cout评估参数的顺序
我现在已经知道了!这是'cout' 的全部代码:
#include <iostream>
using namespace std;
int main()
{
int i = 3;
cout <<-i++<<endl<<i<<endl<<-(i++)<<endl<<i<<endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我用VC++ 6.0编译,输出是:-4 4 -3 3
但我用g ++编译,输出是:-4 5 -3 5
为什么?我认为他们应该是一样的:-4 4 -4 4
PS:我试试看:
int main()
{
int i = 3;
cout <<-i++<<endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
和
int main()
{
int i = 3;
cout <<-?i++?<<endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我逐个编译它们,结果是一样的:-3 3想到所有的答案,我可能有一个错误的测试-i ++和 - (i ++)莫名其妙