小编gen*_*ult的帖子

为什么STL Map中的值没有变化?

如果值大于1,我将map(键值对)中的值减1

#include <bits/stdc++.h>
using namespace std;
int main()
{
     // Creating a map with 4 element
    map<int,int> m;
    m[1]=1;
    m[2]=2;
    m[3]=1;
    m[4]=3;
     //Printing the output
    for(auto x: m)cout<<x.first<<" "<<x.second<<endl;
    //Applying substraction
    for(auto x: m)
    {
        if(x.second>1)
        {
            x.second--;
        }
    }
    cout<<"After subtraction operation: \n";
    for(auto x: m)cout<<x.first<<" "<<x.second<<endl;

}
Run Code Online (Sandbox Code Playgroud)

产量

c++ dictionary stl

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

GDI +闪烁

所以我想制作一个廉价的Gyazo副本(截图工具)

问题是光标坐标是闪烁的,我该如何防止?我已经尝试过,WM_ERASEBKGND但它没有任何帮助.

我的代码还有什么问题吗?任何不良做法/技巧?

#include <Windows.h>
#include <string>
#include <gdiplus.h>
#pragma comment (lib, "Gdiplus.lib")

// Store the "screenshot" when first launching the program
HBITMAP hbm;

// This draws the cursor coordinates close to the cursor
void DrawCursorCoords(Gdiplus::Graphics &graphics, Gdiplus::Bitmap &bitmap, Gdiplus::Color c)
{
    POINT cursorPos;
    GetCursorPos(&cursorPos);

    std::wstring x = std::to_wstring(cursorPos.x);
    std::wstring y = std::to_wstring(cursorPos.y);

    graphics.DrawString(x.c_str(), x.length(), &Gdiplus::Font(L"Consolas", 16), Gdiplus::PointF(cursorPos.x, cursorPos.y), &Gdiplus::SolidBrush(c));
    graphics.DrawString(y.c_str(), y.length(), &Gdiplus::Font(L"Consolas", 16), Gdiplus::PointF(cursorPos.x, cursorPos.y + 16), &Gdiplus::SolidBrush(c));
}

// Paint our stuff
void Paint(HDC &hdc)
{ …
Run Code Online (Sandbox Code Playgroud)

c++ winapi gdi+ flicker double-buffering

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

csv列中数百万个数据值的频率

我有一长串的数字(一列有500万行),并不是彼此独特的.我想看看它们中有哪千个是列表中最常出现的事件.关于如何轻松实现这一目标的任何想法?我也可以使用excel或python脚本或其他方法.

python csv excel frequency histogram

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

OpenGL 的无损纹理压缩

我有几个 32 位(带 alpha 通道)位图图像,我将它们用作游戏中的基本信息。RGBA 值的微小变化会破坏一切,所以我不能使用像 S3TC 这样的有损压缩方法。

是否有任何可行的无损压缩算法可以与 OpenGL 一起使用?我正在使用片段着色器,我想使用glCompressedTexImage2D()方法来定义纹理。我还没有尝试过使用GL_COMPRESSED_RGBA参数用 OpenGL 压缩纹理,我有没有机会通过这种方式获得无损压缩?

compression opengl textures lossless-compression image-compression

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

我在C++中尝试使用SDL_RenderCopy复制纹理时出错

这是我的.cpp文件:

#include  <iostream>
#include  <SDL2/SDL.h>
#include  <SDL2/SDL_ttf.h>

bool runningOnEmpty = false;
const char* title;
int xpos, ypos, width, height;
bool fullscreen = false;
SDL_Window *window;
SDL_Renderer *renderer;

void init(){
  int flags = 0;
  if(fullscreen){
    flags = SDL_WINDOW_FULLSCREEN;
  }

  if(!SDL_Init(SDL_INIT_EVERYTHING)){
    std::cout << "Sdl initialised!!\n";

    window = SDL_CreateWindow(title, xpos, ypos, width, height, flags);
    if(window){
      std::cout << "window created!!\n";
    }

    renderer = SDL_CreateRenderer(window, -1, 0);
    if(renderer){
      std::cout << "renderer created!!\n";

      SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
    }

    runningOnEmpty = true;
  } else {
    runningOnEmpty …
Run Code Online (Sandbox Code Playgroud)

c++ sdl-ttf sdl-2

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

函数名称表示什么以及更多

考虑这个简单的程序:

#include <iostream>

void length(void){
    std::cout<<"My length is void"<<std::endl;
}

void width(void){
    std::cout<<"My width is void"<<std::endl;
}

int main(void){
    std::cout<<"The program length is: "<<length<<std::endl;
    std::cout<<"The program width is: "<<width<<std::endl;
}
Run Code Online (Sandbox Code Playgroud)

该程序的输出是:

The program length is: 1
The program width is: 1
Run Code Online (Sandbox Code Playgroud)

程序打印出来的数字是多少,基本上没有多少C++知识,这对我来说看起来更加pythonic语法,看起来好像应该打印函数地址.我这样说是因为这个问题最初是在为openGL练习一些非常基本的程序时出现的,

回调是在GLUT中注册的,例如:

void line(void){
   //some code for line program ...
}

int main(int argc, char** argv){
    //some more code;
    glutDisplayFunc(line);
    glutMainLoop();
}    
Run Code Online (Sandbox Code Playgroud)

这几乎看起来好像我们正在传递函数的地址,但是,从上面的程序中可以清楚地知道这不是一个地址,指向函数的语法有点不同,如果是这样,这个函数如何被注册为打回来?我们传递的是 glutDisplayFunc什么?

并且,因为我想要注册一个已经传递参数的函数,我搜索python lambda函数的C++类比,并找到类似的lambda函数,但它没有成功:**

#include <iostream>

void line(int a,  int b){
//some code that does …
Run Code Online (Sandbox Code Playgroud)

c++ opengl glut

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

std :: function与标准函数

我正在使用函数指针std :: function, 并遇到了以下问题.

让我们考虑下面的代码:

#include <cmath>
#include <functional>

// g++ -std=c++17 SF.C -o SF
// clang++ -std=c++17 SF.C -o SF

int main()
{
    typedef double (*TpFunctionPointer)(double) ;

    TpFunctionPointer pf1 = sin;                     // o.k.
    TpFunctionPointer pf2 = std::sin;                // o.k
    TpFunctionPointer pf3 = std::riemann_zeta;       // o.k

    std::function< double(double) > sf1( sin );                // o.k
    std::function< double(double) > sf2( std::sin );           // fails
    std::function< double(double) > sf3( std::riemann_zeta );  // fails
}
Run Code Online (Sandbox Code Playgroud)

用函数指针pf1,pf2,pf3和sf1 编译g++ v8.2clang …

c++ std c++17

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

如何为定制结构配置VAO和VBO

我正在尝试建造一个酒吧。

首先我声明一个结构

struct VertexFormat
    {
        glm::vec3 positions;
        glm::vec3 normal;
        glm::vec2 texCoord;
        VertexFormat(glm::vec3 pos, glm::vec3 norm, glm::vec2 coord) : positions(pos), normal(norm), texCoord(coord)
        {   }
    };
Run Code Online (Sandbox Code Playgroud)

然后我配置 VAO 和 VBO

glBindVertexArray(m_VAO);
glBindBuffer(GL_ARRAY_BUFFER, m_VBO);
glBufferData(GL_ARRAY_BUFFER, data.size() * sizeof(VertexFormat), &data[0], GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size(), &indices[0], GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, data.size() * sizeof(VertexFormat), (void*)0);
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, data.size() * sizeof(VertexFormat), (void*)(3 * sizeof(float)));
glEnableVertexAttribArray(2);
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, data.size() * sizeof(VertexFormat), (void*)(6 * sizeof(float)));
glBindVertexArray(0);
Run Code Online (Sandbox Code Playgroud)

当我发出绘图命令时,没有绘制任何内容

glBindVertexArray(m_VAO);
glDrawElements(GL_TRIANGLES, 24, GL_UNSIGNED_INT, …
Run Code Online (Sandbox Code Playgroud)

c++ opengl glm-math

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

如何用sdl2快速绘制像素网格?

我有以下函数,可以在窗口上绘制像素网格,我正在使用 sdl。

问题是速度太慢了!它使我的程序以 10fps 运行,所以我想我一定做错了什么。

这是我正在使用的代码

void rayTracing(SDL &sdl) {
  int nx = 1440;
  int ny = 810;

  for (int x = 0; x < nx; x++) {
    for (int y = 0; y < ny; y++) {
      float r = float(x) / float(nx);
      float g = float(y) / float(ny);
      float b = 0.2;
      int ir = int(255.99 * r);
      int ig = int(255.99 * g);
      int ib = int(255.99 * b);

      SDL_SetRenderDrawColor(sdl.renderer.get(), ir, ig, ib, 255);
      SDL_RenderDrawPoint(sdl.renderer.get(), x, ny …
Run Code Online (Sandbox Code Playgroud)

c++ sdl-2

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

创建一个猜数字游戏

我正在尝试创建一个游戏,其中用户输入一个数字作为间隔的最大数字,然后程序要求用户猜测一个随机数。当我测试它时,用户可以继续猜测,但没有最终正确的随机数。

int main() {
    int num, intervalMax, guess; // Define variables
    srand(time(0)); // Give a "random" seed to the random number generator

    cout << "Let's generate some random numbers in the interval [1, 10]" << endl;
    for (int i = 0; i < 5; i++) {
        num = rand() % 10 + 1;
        cout << "Your random number is " << num << "!" << endl;
    }

    cout << endl;
    cout << "Great, now it's time to play a guessing …
Run Code Online (Sandbox Code Playgroud)

c++

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