我试图谷歌搜索我的问题的答案,但我找不到任何有效的解释,因此我在这里发布我的问题.以下是我的示例代码和输出:
#include <iostream>
#include "vector"
using namespace std;
typedef struct Node{
int data;
Node(){
data = 0;
std::cout << "Node created. " << this <<'\n';
}
~Node(){
data = 0;
std::cout << "Node destroyed. " << this <<'\n';
}
} Node;
int main() {
std::vector<Node> vec;
for(int i = 0; i < 2 ; i++)
vec.push_back( *(new Node));
return 0;
}
Run Code Online (Sandbox Code Playgroud)
Node created. 0x9e0da10
Node created. 0x9e0da30
Node destroyed. 0x9e0da20
Node destroyed. 0x9e0da40
Node destroyed. 0x9e0da44
Run Code Online (Sandbox Code Playgroud)
为什么会有一个额外的破坏以及为什么创建的对象与被破坏的对象不同?
我面临着一个奇怪的问题,我在我的机器上的g ++ cygwin编译器上编译了我的代码,并将其输出与我从ideone获得的输出进行了比较,奇怪的是两者都不同.
下面是我的代码:
#include <cstdlib>
#include "queue"
#include "iostream"
using namespace std;
class Student{
private:
int rollNum;
public:
Student(int id):rollNum(id){
}
int getRoll(void){
return rollNum;
}
};
#if 1
int main(int argc, char** argv) {
priority_queue<Student* , vector <Student*> , less<Student*> > pq;
for(int i = 0; i < 10 ; i++){
pq.push(new Student(i));
}
for(; !pq.empty();){
cout << pq.top()->getRoll() << '\n';
pq.pop();
}
return 0;
}
#endif
Run Code Online (Sandbox Code Playgroud)
当我在我的本地系统上运行时,我得到输出:8 7 6 9 4 5 3 1 2 0
在任何c …
在c ++中我们std::to_string将int/float/double转换为字符串.所以,为了测试我对模板的理解,我尝试了下面的代码:
#include "iostream"
#include "sstream"
#include "string"
using std::cout;
template <typename T>
std::string getString(const T& data){
std::stringstream ss;
cout << '\n' << data << '\n';
ss << data;
std::string s;
ss >> s;
return s;
}
int main(int argc , char** argv){
cout << getString(1.0000011);
cout <<' '<<std::to_string(1.0000011);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
然而,输出没有意义,to_string给我1.0000011,而getString得到1并给我1.因为我使用模板也不应该getString得到1.0000011并给我1.0000011?
我一直在玩STL容器和它们支持的比较函数/仿函数,但是我发现priority_queue不遵循通常的严格弱序,我试图理解可能是什么原因但不能弄明白,任何指针会有所帮助.
它在本博客中还提到priority_queue不遵循严格的弱排序.在此输入链接描述
#include "STL.h"
#include "queue"
#include "vector"
#include "iostream"
#include "functional"
using namespace std;
typedef bool(*func)(const int& val1 , const int& val2);
bool strict_weak_order_function(const int& val1 , const int& val2){
return val1 > val2;
}
bool comparer_function(const int& val1 , const int& val2){
return !strict_weak_order_function(val1 , val2);
}
struct Compaper_functor{
bool operator()(const int& val1 , const int& val2){
return !strict_weak_order_function(val1 , val2);
}
};
void runPriorityQueue(void){
//priority_queue<int , vector<int> , func > pq(comparer_function);
priority_queue<int , vector<int> , Compaper_functor …Run Code Online (Sandbox Code Playgroud) 我已经在C++上工作了几年,已经编写了几次东西,但是下面这个问题对我来说是全新的,它只是没有意义.
以下是我遵循的步骤:
这里有什么可能出错的指针?
以下是编译的代码:
#include "iostream"
#include "thread"
#include "mutex"
using namespace std;
class threadFunctor{
public:
};
int main(int argc , char** argv){
cout << "Hello";
return 0;
}
Run Code Online (Sandbox Code Playgroud)
以下代码给出错误:
#include "iostream"
#include "thread"
#include "mutex"
using namespace std;
class threadFunctor{
public:
};
int main(int argc , char** argv){
cout << "Hello World";
return 0;
} …Run Code Online (Sandbox Code Playgroud)