小编Que*_*tin的帖子

使用std :: mutex进行死锁模拟

我有以下示例:

template <typename T>
class container
{
public:
    std::mutex _lock;
    std::set<T> _elements;

    void add(T element)
    {
        _elements.insert(element);
    }

    void remove(T element)
    {
        _elements.erase(element);
    }
};

void exchange(container<int>& cont1, container<int>& cont2, int value)
    {
        cont1._lock.lock();
        std::this_thread::sleep_for(std::chrono::seconds(1));

        cont2._lock.lock();

        cont1.remove(value);
        cont2.add(value);

        cont1._lock.unlock();
        cont2._lock.unlock();
    }

    int main() 
    {
        container<int> cont1, cont2;

        cont1.add(1);
        cont2.add(2);

        std::thread t1(exchange, std::ref(cont1), std::ref(cont2), 1);
        std::thread t2(exchange, std::ref(cont2), std::ref(cont1), 2);

        t1.join();
        t2.join();

        return 0;
    }
Run Code Online (Sandbox Code Playgroud)

在这种情况下,我正在陷入僵局.但是当我使用std :: lock_guard而不是手动锁定和解锁互斥锁时,我没有死锁.为什么?

void exchange(container<int>& cont1, container<int>& cont2, int value)
{
    std::lock_guard<std::mutex>(cont1._lock);
    std::this_thread::sleep_for(std::chrono::seconds(1));

    std::lock_guard<std::mutex>(cont2._lock);

    cont1.remove(value);
    cont2.add(value); …
Run Code Online (Sandbox Code Playgroud)

c++ mutex stdthread

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

"C++库不会为这种类型提供哈希值." - 在std :: unordered_map中使用自己的类

我正在尝试编写康威的"生命游戏".在接近我的目标时,我遇到了编译错误:

C2338:C++库不提供此类型的哈希值.

起初我使用了SFML类sf::Vector2D.当它无法为我工作时,我写了一个属于我自己的类,希望我能实现丢失的hashCode方法.

我的问题是:
是否可以使用我自己的类使用自己的hashCode方法std::unordered_map?我需要使用一个可以容纳两个数字的类.(我也尝试过std::tuple,但是struct东西).

这是我的一张代码:

#include "GameMechanics.h"



GameMechanics::GameMechanics(Elements * elements):elements(elements)
{
    this->refreshTime = 1000000;    //ms
    this->clock.restart();
}


GameMechanics::~GameMechanics()
{
}

bool GameMechanics::isRunning()
{
    return this->running;
}

void GameMechanics::setRunning(bool running)
{
    this->running = running;
}

void GameMechanics::loop()
{

    unsigned passedTime = clock.getElapsedTime().asMicroseconds();  //check passed time since the clock got restarted
    this->timeHeap  +=  passedTime; //add passed time to the timeheap
    this->clock.restart();
    //only refresh every "refreshTime" seconds
    if (timeHeap >= …
Run Code Online (Sandbox Code Playgroud)

c++ unordered-map sfml

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

具有约束/概念的类模板专业化

我尝试使用约束进行类专门化:

struct A {};
struct B {};
struct C {};

template<typename T>
concept bool AorB() {
    return std::is_same<T, A>::value || std::is_same<T, B>::value;
}

template<typename T>
class X {};

template<AorB T>
class X {};

int main() {
    X<A> x1; // error: redeclaration 'template<class T> class X' with different constraints class X {};
    X<B> x2;
    X<C> x3;
}
Run Code Online (Sandbox Code Playgroud)

我不知道我在这里犯了错误,或者这一般是不可能的?

有什么可以替代这种方法?我可以使用CRTP到一个公共基本模板的专业化,但这对我来说看起来很难看.

c++ c++-concepts c++17

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

如何在C++ 11的迭代器中获得有关auto标识符的更多细节

 void test()
 {
       vector <string> s={"hello world"};
       for(vector<string>::iterator it=s.begin();it!=s.end()&&!it->empty();it++)
      {
           for(auto it2=it->begin();it2!=it->end();it2++)//I don't konw  type of it2
           {
                *it2=toupper(*it2);
           }
       cout<<*it<<endl;
     }
}
Run Code Online (Sandbox Code Playgroud)

在第一个循环中,我可以确定类型的迭代器是vector<string>::iterator.我想知道什么是类型it2(我已经尝试过使用vector<string>::const).我怎样才能获得关于哪种类型auto相同的更多细节.

c++ c++11

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

c ++将类方法作为参数传递给带有模板的类方法

我正在尝试使用模板将类方法传递给另一个类方法,并且找不到任何关于如何做的答案(没有C++ 11,提升确定):

我将核心问题简化为:

class Numerical_Integrator : public Generic Integrator{
    template <class T>
    void integrate(void (T::*f)() ){
         // f(); //already without calling  f() i get error
    }
}

class Behavior{
    void toto(){};

    void evolution(){
        Numerical_Integrator my_integrator;
        my_integrator->integrate(this->toto};
}
Run Code Online (Sandbox Code Playgroud)

我得到了错误:

error: no matching function for call to ‘Numerical_Integrator::integrate(<unresolved overloaded function type>)’this->toto);
note:   no known conversion for argument 1 from ‘<unresolved overloaded function type>’ to ‘void (Behavior::*)()’
Run Code Online (Sandbox Code Playgroud)

谢谢.

奖金:争论怎么样?

class Numerical_Integrator{
    template <class T, class Args>
    double integrate(void (T::*f)(), double a, Args arg){ …
Run Code Online (Sandbox Code Playgroud)

c++ templates member-functions c++03

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

如果编译得很好,`void main`怎么会无效?

我找到了int main定义,但没有void main介绍c ++编程语言.我试着阅读所有用c ++编程语言介绍的文章.

c c++ program-entry-point

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

当我打电话给我的功能时,什么都没发生?

我正在尝试使用两个char参数来制作摇滚,纸张,剪刀游戏的功能,其中第一个参数代表用户选择的摇滚,纸张或剪刀.第二个参数代表游戏的结果,胜利,失败或平局.但是,当我试图调用该函数时,没有任何事情发生.我迷失在接下来我需要做什么.非常感谢所有帮助!

#include <iostream>
#include <cstdlib>
using namespace std;

double playRPS (char a, char b);


int main() {

    char letter;
    char result = 0;

    cout << "Welcome to COP3014 ROCK PAPER SCISSORS!\n\n";

    cout << "Please select: " << endl
         << "Rock(r), Paper(p), or Scissors(s)? " << endl
         << "Or enter q to quit --> ";

    cin >> letter;

    if (letter == 'r' || letter == 'R' || letter ==  'p' || letter == 'P'     || letter == 's' || letter == …
Run Code Online (Sandbox Code Playgroud)

c++ function

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

使用`&`和`(int*)`打印char*有什么区别?

我正在学习OOP课程,但我是C++的新手.我的导师向我们介绍了C++中的字符数组.他说,要在C++中获取字符数组的基址,可以使用以下任一方法:

char* a = "Test String";
cout << &a; // Prints base address of char array
Run Code Online (Sandbox Code Playgroud)

要么

char* a = "Test String";
cout << (int*)a;
Run Code Online (Sandbox Code Playgroud)

但当我尝试两个相同的char数组时,我得到了不同的结果.两者有什么区别?

c++ arrays pointers

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

使用`transform`创建一个增加的向量

我试图使用增加的向量transform,但不能正确地做.我想用变换.我究竟做错了什么?

PS - 我将使用c ++ 11标准和g ++.

#include <iostream>
#include <algorithm>
#include <vector>

int main()
{
    std::vector<double> x(10);
    x.front() = 0.0;
    double h = 0.1;
    std::transform(x.begin(), x.end() - 1, x.begin() + 1, [h](unsigned int xn) {return xn + h;});
    std::cout << x.at(3) << " " << x.at(9) << std::endl;
}
Run Code Online (Sandbox Code Playgroud)

c++ std c++11

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

C ++可以使用移动语义将数据从一个向量移动到另一个向量

下面的代码将数据从一个向量复制到另一个向量。如果向量很大,那么我猜这很昂贵。是否可以在此处使用移动语义将数据从一个向量复制到另一个向量?

std::vector<double> newData(Shape.Size(), 0);
std::vector<double> oldData = a->getData();
std::copy(oldData.begin(), oldData.end(), newData.begin());
Run Code Online (Sandbox Code Playgroud)

c++ vector move-semantics c++11

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