小编Aru*_*rup的帖子

std :: vector内存处理

我试图谷歌搜索我的问题的答案,但我找不到任何有效的解释,因此我在这里发布我的问题.以下是我的示例代码和输出:

#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)

为什么会有一个额外的破坏以及为什么创建的对象与被破坏的对象不同?

c++ vector

22
推荐指数
3
解决办法
1918
查看次数

C++ STL的系统级依赖性

我面临着一个奇怪的问题,我在我的机器上的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++

1
推荐指数
1
解决办法
84
查看次数

需要帮助来了解字符串的错误

在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?

c++ string templates stl

1
推荐指数
1
解决办法
93
查看次数

为什么STL中的priority_queue不遵循严格的弱排序?

我一直在玩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++ stl priority-queue strict-weak-ordering

0
推荐指数
1
解决办法
243
查看次数

cpp文件没有在clang ++和g ++中编译

我已经在C++上工作了几年,已经编写了几次东西,但是下面这个问题对我来说是全新的,它只是没有意义.

以下是我遵循的步骤:

  • 使用cygwin设置与g ++版本:6.4.0和clang ++版本:4.0.1
  • 创建了一个新的cpp fie,使用sublime文本添加简单的cout并
    使用以下命令编译:clang ++ -g -Wall -std = c ++ 14 thread1.cpp -o thread,工作正常.
  • 添加新的内容可能是另一个cout,这次编译时我得到了大量的错误,说明它不是utf-8文件.
  • 在sublime文本中使用utf-8编码保存文件,并尝试使用utf-8 BOM编码,仍然获得相同而不是utf-8文件错误.
  • 在cygwin中运行file命令检查文件编码,文件-i thread1.cpp,输出为thread1.cpp:text/xc; 字符集= UTF-8.

这里有什么可能出错的指针?

以下是编译的代码:

#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)

c++ encoding utf-8 clang c++11

0
推荐指数
1
解决办法
334
查看次数