标签: c++17

将常量局部变量定义为静态(c++)有什么优点和缺点?

void Animation::playAnimation() const
{
    static const int index = 0;
    const std::string& animationFileName = m_animationContainer.getAnimationName(index);
    static const int zOrder = -1;
    static bool isLooping = false;

    AnimationBank::play(animationFileName,
                        zOrder,
                        isLooping);
}
Run Code Online (Sandbox Code Playgroud)

将常量局部变量定义为静态的优点和缺点是什么?将index, zOrder,定义为 的开销是多少。这样做有什么好处吗?isLoopingstatic

c++ c++11 c++14 c++17

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

C ++ 17文件系统copy_file访问被拒绝

我正在使用 Visual Studio 2017,运行 c++17 ISO 标准(不是 boost)设置为能够使用<filesystem>. 不过,我遇到了麻烦,因为每次运行时,无论是在调试还是发布中,file_copy()都会出现访问被拒绝的错误。我检查了代码的其他部分,唯一不起作用的是file_copy(). 有谁知道我为什么会收到此错误以及如何修复它?我是我电脑上的管理帐户。

std::vector<std::string> findAndCopyFiles()
{
    std::vector<std::string> fileNames;
    std::error_code errCode;
    errCode.clear();

    fs::current_path("C:\\Users\\kenny\\Desktop\\Engine", errCode);
    std::cout << errCode.message() << std::endl; errCode.clear();

    fs::path pa = fs::current_path();
    pa += "\\TEMP";
    std::cout << pa.string() << std::endl;

    if (fs::create_directory(pa, errCode))//Create directory for copying all files)
    {
        std::cout << "Directory created successfully" << std::endl;
        std::cout << errCode.message() << std::endl; errCode.clear();
    }
    fs::path tempDir(pa);
    fs::path currentDirectory = fs::current_path();
    fs::recursive_directory_iterator dirIter(currentDirectory);
    for (auto &p : dirIter) …
Run Code Online (Sandbox Code Playgroud)

c++ filesystems c++17

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

包装生成随机数的函数的最简单和最优雅的方法是什么?

函数如下:

float random_float(float min, float max)
{
    std::random_device rd;                                            // obtain a random number from hardware
    std::mt19937 gen(rd());                                           // seed the generator
    std::uniform_real_distribution<> distr((double)min, (double)max); // define the range
    return (float)distr(gen);
}
Run Code Online (Sandbox Code Playgroud)

我想避免每次调用此函数时调用前 3 行。我也不想用构造函数创建一个类,然后每次我只想生成一个随机数时都必须实例化它。我不太熟悉现代 C++ 特性的可能性,所以我想要一些想法。

c++ c++17

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

字符串不节省空格?

我正在做一些你必须输入你的名字的东西,但我的字符串没有保存空格,为什么?

#include <iostream>
using namespace std;
string name;
int main()
{
  cout<<"<System>: Please enter your name"<<endl;
  cin>>name;
  cout<<name;
return 0;
}
Run Code Online (Sandbox Code Playgroud)

我进入了:

测试123

我得到了:

测试

c++ c++17

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

C++ 如何简化 if else 语句?

我想知道如何简化如下所示的声明。

我到处都有类似的代码,想清理它。

if(isActive)
{
    if(columnId == 4)
        g.drawText(active[row].value, 2, 0, width, height, Justification::centredLeft, true);
}
else
{
    if(columnId == 4)
        g.drawText(inactive[row].value, 2, 0, width, height, Justification::centredLeft, true);
}
Run Code Online (Sandbox Code Playgroud)

isActive正如您可以想象的那样,是一个bool值。

c++ coding-style c++11 c++17

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

编译器错误?在 std::tuple 中返回 std::vector 和 std::string。但我得到了奇怪的价值观

我想返回多个值并使用 声明该函数auto

但这效果并不好。无法正确返回值。它被覆盖了。

我尝试执行以下功能f1f3。这些函数应该返回元组中的向量和字符串。但只是f3效果很好。

#include <iostream>
#include <vector>
#include <string>
#include <tuple>

auto f1(){
    std::vector<double> v(10, 0);
    std::string s = "hello";
    return std::forward_as_tuple(v, s);
}

auto f2(){
    std::vector<double> v(10, 0);
    return std::forward_as_tuple(v, "hello");
}

std::tuple<std::vector<double>, std::string> f3(){
    std::vector<double> v(10, 0);
    std::string s = "hello";
    return std::forward_as_tuple(v, s);   
}
int main(void){
    //change the function
    //auto [vec, str] = f1();
    //auto [vec, str] = f2();
    auto [vec, str] = f2();

    for (auto e : …
Run Code Online (Sandbox Code Playgroud)

c++ stdvector auto stdtuple c++17

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

如何立即暂停一个线程?

假设有两个线程,其中一个线程具有更高的优先级,并且它们在同一核心(单核)上运行,我只想同时只工作一个线程。(也许你说这不是线程范例,但实际上我只是做了我的这里问题最小)

T1 ~~~e1~e2~e3~e4~...~~~~~~~eK~~~~~~~~...~~~ eN~~~~~ ///(e1...eN)packed as a task.
                |            |
T2 ~~~~~~~~~~~~pause~~~~~~continue~~~~~~~~~~~~~~~~~~ ///(pause & continue)is just title time for T1 that T2 is operating (T2 has more priority).
Run Code Online (Sandbox Code Playgroud)

**~**是 time ,并且**e**是在那里求值的表达式。整个 e1、e2、... 是一个函数,即 api 调用函数(任务),所以我只想在那里暂停 T1(~pause~)并运行我的 T2 直到完成,完成后继续 T1。

注意:我无法更换**e**工作(职能)。

我知道的?

创建条件变量(CV)并在完成时T2通知CV唤醒,但这不是我的成就,因为我想让T1立即在e4 暂停时间)中精确暂停并继续T2直到完成(或我的继续时间)。

我的知识与: https: //en.cppreference.com/w/cpp/thread/condition_variable#Example 我们是否有立即暂停的 thread::方法(强制上下文切换)?(我不是说屈服!)

c++ multithreading c++17 conditional-variable

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

字符串数据类型为 C++ 程序中的特定测试用例创建运行时错误

对于codeforces问题:

https://codeforces.com/problemset/problem/339/A

以下程序在第七次测试中显示运行时错误:

#include<iostream>
using namespace std;

int main()
{
    char s[100];
    cin>>s;
    int i;
    string a,b,c;
    int j=0,k=0,l=0;
    for(i=0;s[i]!='\0';i++)
    {
        if(s[i]=='1')
        {
            a[j]='1';
            a[j+1]='+';
            j+=2;
        }

        if(s[i]=='2')
        {
            b[k]='2';
            b[k+1]='+';
            k+=2;
        }

        if(s[i]=='3')
        {
            c[l]='3';
            c[l+1]='+';
            l+=2;
        }
    }
    char str[100];
    int x,y,z;
    for(x=0;x<=j-1;x++)
    {
        str[x]=a[x];
    }

    for(y=0;y<=k-1;y++,x++)
    {
        str[x]=b[y];
    }

    for(z=0;z<=l-1;z++,x++)
    {
        str[x]=c[z];
    }

    int q;
    for(q=0;q<=x-2;q++)
    {
        cout<<str[q];
    }

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

测试用例是:

2+3+3+1+2+2+2+1+1+2+1+3+2+2+3+3+2+2+3+3+3+1+1+1+3+ 3+3+2+1+3+2+3+2+1+1+3+3+3+1+2+2+1+2+2+1+2+1+3+1+1

string a,b,c;当我更改为时答案被接受char a[100],b[100],c[100];

我无法理解为什么string这里的数据类型导致运行时错误。

我猜这可能是由于测试用例的长度造成的,但我不确定为什么。

c++ arrays string runtime-error c++17

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

在 C++ 中将 float 转换为 int

int uniquePaths(int m, int n) {
    int num = m+n-2;
    int den=1;
    double ans = 1;
    while(den<=m-1) {
        ans = ans*(num--)/(den++);
    }
    cout<<ans;
    return (int)ans; 
}
Run Code Online (Sandbox Code Playgroud)

作为上述代码段的输入,m=53, n=4 的预期答案是 26235,但代码返回 26234。但是,标准输出显示 26235。

您能帮我理解这种行为吗?

c++ c++17

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

为什么 x&amp;1==0 和 !(x&amp;1) 在 C++ 的 if 语句中不会产生相同的结果?

我在语句中包含这两个条件if来检查是x偶数还是奇数,但似乎在偶数的情况下!(x&1)执行了主体,而没有执行它。ifxx&1==0

考虑到32 或 64 位表示中的1 & 0is和 1 ,我希望两者都给出 0 ,并且如果,比如说,是类似(偶数)的东西,那么它们的按位 和 应该产生 0。因此,我仍然不确定为什么有效。如果我有任何错误的地方,请纠正我。谢谢。0000..01x10010101100!(x&1)

c++ bitwise-operators c++17

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