小编And*_*dyG的帖子

How to draw a sine wave in OpenGL_POINTS Function using C++

在与红色小点的2PI被画的正弦波的图象 I'm supposed to draw a sine wave (like the one in the image) using OpenGL_POINTS. However, after going through my loop in the code, I keep getting just one point of the wave.

Here's my code.

#include "stdafx.h"
#include <iostream>
#include <gl\GLUT.h>
#include <math.h>

using namespace std;

void RenderSineWave()
{
    int i;  
float x,y;  
glClearColor(0.0, 0.0, 0.0, 1.0);  // clear background with black
glClear(GL_COLOR_BUFFER_BIT);   

    glPointSize(10);
    glColor3f(1.0,0.0,0.0);


        for(i=0;i<361;i=i+5)
        {

            x = (float)i; 
            y = 100.0 * sin(i *(6.284/360.0));
            glBegin(GL_POINTS);
            glVertex2f(x,y);
            glEnd(); …
Run Code Online (Sandbox Code Playgroud)

c++ opengl

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

模板化赋值运算符模板实例化失败

我正在尝试构建一个模板化的基类,允许分配给它的任何模板类型,如下所示:

#include <type_traits>
#include <utility>

template<typename T1, typename... Types>
class Base
{
    public:
    //assignment to base or derived class
    template<typename T, typename std::enable_if<std::is_base_of<Base<T1, Types...>, T>::value>::type = 0>
    Base& operator=(T&& other)
    {
        if (this != &other)
            a = other.a;
        return *this;
    }

    //assignment to other type contained in <T1, Types...>
    template<typename T, typename std::enable_if<!std::is_base_of<Base<T1, Types...>, T>::value>::type = 0>
    Base& operator=(T&& other)
    {
        // do some stuff
        return *this;
    }


    private:
    int a;
};
Run Code Online (Sandbox Code Playgroud)

正如您所看到的,我正在尝试使用包装std::enable_if来区分赋值Base(移动或复制)和赋值给类型std::is_base_of.我的印象中T&& …

c++ templates c++11

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

在编译时比较两个整数序列?

假设我有一个constexpr std::integer_sequence<...>对象.在编译时,我对它执行一些操作,然后我想static_assert它是==另一些std::integer_sequence<...>.鉴于这integer_sequence是一种类型,我如何提供一个constexpr bool operator==适当比较它们的重载 ?

一个更具体的例子:转换intstd::integer_sequence<char>.也就是说,将整数转换为字符序列(受到Peter Sommerlad在CPPCon '15的演讲的启发)

我有一些功能,我非常有信心将小于1000的十进制整数值适当地转换为4个元素的字符序列:

#include <utility> // integer_sequence

template<char... t>
using char_sequence = std::integer_sequence<char, t...>;
constexpr char make_digit_char(const size_t digit, const size_t power_of_ten=1, const char zero_replacement = ' ')
{
    return char(digit>=power_of_ten?digit/power_of_ten+'0':zero_replacement);
}

template<int num>
constexpr auto int_to_char_sequence()
{
    static_assert(num < 1000, "Cannot handle integers larger than 1000!");
    //format for up to 1000
    return char_sequence<' ', 
                    make_digit_char(num,100), …
Run Code Online (Sandbox Code Playgroud)

c++ templates variadic-templates c++14

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

通过索引访问矢量迭代器?

最近我在我的代码库中遇到了这个代码(当然是简化的)

auto toDelete = std::make_shared<std::string>("FooBar");
std::vector<decltype(toDelete)> myVec{toDelete};
auto iter = std::find_if(std::begin(myVec), std::end(myVec), 
   [](const decltype(toDelete) _next)
   {
      return *_next == "FooBar";
   });

if (iter != std::end(myVec))
{
   std::shared_ptr<std::string> deletedString = iter[0];
   std::cout << *deletedString;
   myVec.erase(iter);
}
Run Code Online (Sandbox Code Playgroud)

在线示例

现在,我注意到我们在这里通过索引访问迭代器!

std::shared_ptr<std::string> deletedString = iter[0];
Run Code Online (Sandbox Code Playgroud)

我以前从未见过有人通过索引访问迭代器,所以我可以猜到的是迭代器被视为指针,然后我们访问指向指针的第一个元素.那么代码实际上相当于:

std::shared_ptr<std::string> deletedString = *iter;
Run Code Online (Sandbox Code Playgroud)

或者是未定义的行为?

c++ iterator random-access dereference c++11

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

需要C信号代码说明

我们在教科书中遇到了这段代码,除了处理信号这一事实外,还没有解释.

#include <signal.h> 
void (*signal(int signr,
            void(*sighandler)(int)
        )
    )(int)
Run Code Online (Sandbox Code Playgroud)

我知道这sighandler是一个函数的指针,但我不明白它是否实际执行或只是返回?

和电话有(int)什么关系?它看起来几乎像一个反转的演员.

c

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

使用 C++20 的 std::popcount 和向量优化是否等同于 popcnt 内在?

C++20 引入了许多新函数,例如std::popcount,我使用Intel Intrinsic使用相同的功能。

我编译了这两个选项 - 可以在编译器资源管理器代码中看到:

  1. 使用英特尔的 AVX2 内在
  2. 使用 std::popcount 和 GCC 编译器标志“-mavx2”

除了 std 模板中使用的类型检查之外,生成的汇编代码看起来是相同的。

就操作系统不可知代码并具有相同的优化而言 - 假设使用std::popcount和 apt 编译器向量优化标志比直接使用内在函数更好是否正确?

谢谢。

c++ intrinsics language-lawyer avx2 c++20

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

不使用条件语句检测 -1

你好,我在学校被问过这个问题。我似乎无法弄清楚。

在不使用任何条件语句(if/else/switch:case/while/for 等)的情况下编写 ac 程序,其输出:
1535(整数)如果输入是 -1(整数)
否则它输出输入(如果不是 -1)?

问题是评估我的逻辑技能而不是 C 编程技能。

c algorithm printf comparison-operators

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

找不到我的Python 3.3语法错误

我收到以下错误:

  File "foo.py", line 6
    print "This implementation requires the numpy module."
                                                         ^
SyntaxError: invalid syntax
Run Code Online (Sandbox Code Playgroud)

在这个Python代码中:

#!/usr/bin/python

try:
    import numpy
except:
    print "This implementation requires the numpy module."
    exit(0)

###############################################################################

if __name__ == "__main__":
    R = [
         [1,2,3],
         [4,5,6]
        ]
Run Code Online (Sandbox Code Playgroud)

怎么了?

编辑:我使用Python 3.3

python python-3.x

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

在派生类的构造函数体中调用基础构造函数

我有这个基类:

class BaseException
{
public:
    BaseException(string _message)
    {
        m_message = _message;
    }

    string GetErrorMessage() const
    {
        return m_message;
    }

protected:
    string m_message;
};
Run Code Online (Sandbox Code Playgroud)

和这个派生类

class Win32Exception : public BaseException
{
public:
    Win32Exception(string operation, int errCode, string sAdditionalInfo = "")
    {
        string message = "";
        message += "Operation \"" + operation + "\" failed with error code ";
        message += std::to_string(errCode);

        if (!sAdditionalInfo.empty())
            message += "\nAdditional info: " + sAdditionalInfo;

        BaseException(message);
    }
};
Run Code Online (Sandbox Code Playgroud)

编译器给我以下错误:

错误C2512:'BaseException':没有合适的默认构造函数可用

我知道我可以构建一个非常长的行来构造将在初始化列表中传递给基类的消息,但这种方式似乎更优雅.

我究竟做错了什么?

c++ inheritance constructor

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

为什么没有lock()在死锁的情况下抛出异常

假设我们有以下代码:

#include <mutex>
#include <thread>

std::mutex m;

void foo()
{
    m.lock();
}

int main()
{
    std::thread th(foo);
    m.lock();
    th.join();
}
Run Code Online (Sandbox Code Playgroud)

我知道这段代码包含死锁,但我想知道C++标准中的以下语句:

30.4.1.2互斥体类型[thread.mutex.requirements.mutex]

6表达式m.lock()应格式正确,并具有以下语义:

[...]

12抛出:需要异常时的system_error(30.2.2).

13错误条件:

- (13.1)operation_not_permitted - 如果线程没有执行操作的权限.

- (13.2)resource_deadlock_would_occur - 如果实现检测到会发生死锁.

- (13.3)device_or_resource_busy - 如果互斥锁已被锁定且无法阻塞.

我们可以看到,违反其中一条规则会导致异常:

30.2.2异常[thread.req.exception]

1本条款中描述的某些函数被指定为抛出system_error类型的异常(19.5.7). 如果检测到任何函数的错误条件,或者对操作系统或其他基础API的调用导致错误导致库函数无法满足其规范,则应抛出此类异常

我提供的代码肯定包含死锁.在这种情况下,标准库是否应抛出异常(因为g ++和Visual C++不这样做)?如果没有,为什么?因为从我的观点来看,它似乎属于13.2(resource_deadlock_would_occur)或13.3(device_or_resource_busy)类别.

c++ multithreading c++11 c++14

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