标签: c++14

将常量局部变量定义为静态(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
查看次数

如何在 RHEL 7.4 上安装 C++14

我的问题类似于如何在 RHEL 7.4 上安装 gcc 4.9.2

但是我正在尝试在 Red Hat 7 上获得 C++14 支持,以便我可以安装 mapnik。

我试过了: # yum-config-manager --enable rhel-server-rhscl-7-rpms

安装 gcc,g++ 4.9.2 版: # yum install devtoolset-3-gcc-c++

启用 gcc-4.9, g++-4.9 : $ scl enable devtoolset-3 bash

但我不断得到

C++ 编译器不支持 C++14 标准(-std=c++14),这是必需的。请升级您的编译器

c++ c++14 rhel7

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

如何在其他QMap中迭代QMap

我寻找如何QMap在其他中迭代 a QMap,例如:

QMap<int, QMap<int, QString>> map;
Run Code Online (Sandbox Code Playgroud)

以前我使用简单的 C++std::map和以下代码并且有效:

for(auto it = this->liste.begin(); it != this->liste.end(); it++) {
    for(auto itr = it->second.begin(); itr != it->second.end(); itr++) {
        //It works !!!!!
        //qDebug() << "First : " << itr->first;
        //qDebug() << "Second : " << itr->second;

        //d.setPath(itr->second);

        //qDebug() << "Path :" << itr->second << " Prefix :" << this->prefix << " Nb :" << itr->first;
        process(d.absolutePath(), this->prefix, itr->first);

        this->liste.clear();
    }
}
Run Code Online (Sandbox Code Playgroud)

我的问题是我如何使用QMap而不是std::map为了在循环中使用 the …

c++ qt qlist qmap c++14

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

反正有没有覆盖内置函数?在 C++

所以我试图覆盖函数 max 并且我遇到了很多错误

> call of overloaded 'max(int&, int&)' is ambiguous

> /usr/include/c++/7/bits/stl_algobase.h:219:5: note: candidate: constexpr const _Tp& std::max(const _Tp&, const _Tp&) [with _Tp = int]
     max(const _Tp& __a, const _Tp& __b)
> 
> In file included from /usr/include/c++/7/bits/char_traits.h:39:0,
                 from /usr/include/c++/7/ios:40,
                 from /usr/include/c++/7/ostream:38,
                 from /usr/include/c++/7/iostream:39,
                 from prog.cpp:1:
Run Code Online (Sandbox Code Playgroud)

我的代码:

#include<iostream>

using namespace std;

template <typename T>

T max(T a, T b)
{
    return a > b?a:b;
}

int main()
{
    cout<< max(5,4);

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

有没有办法覆盖内置函数或预定义函数?

即使我声明

int a(5),b(4);
cout<<max(a,b);
Run Code Online (Sandbox Code Playgroud)

它给了我错误

c++ c++14 c++17

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

从 int 到非标量类型“point”的 C++ 转换出现错误:

1.在这段代码中我想构造一个点类型的数组并将其每个索引的值初始化为1,2

我可以实现以下代码吗???

2.如果以下代码不起作用,正确的方法是什么???

#include<iostream>
using namespace std;
class point
{
 private:
   int x,y;
 public:
   point(int a,int b)
   {
       cout<<"constructor called here"<<endl;
       x=a;
       y=b;
   }
};

int main()
{
point p[2]{(1,2),(1,2)}; 
}
Run Code Online (Sandbox Code Playgroud)

c++ c++14

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

如何在c ++中使任何整数为正?

我从用户那里获取 2 个值 a 和 b 并执行 ab 但如果用户的答案是 -ve 那么我想将其转换为正数。

int main(int argc, char const *argv[])
{
    int a,b;
    cin>>a>>b;
    cout<<(a-b);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

怎么做 ?

c++ c++14

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

std::adjacent_find() 和 std::equal() 之间的区别,用于检查向量中的所有元素是否相等

我试图找出以下之间的区别:

return std::adjacent_find(v.begin(), v.end(), std::not_equal_to<>()) == v.end();
Run Code Online (Sandbox Code Playgroud)

return std::equal(v.begin() + 1, v.end(), v.begin());
Run Code Online (Sandbox Code Playgroud)

后者有点短,所以我更喜欢这样写,我发现的唯一区别是它因空数组而崩溃。速度大致相等。

c++ c++11 c++14

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

unordered_map似乎没有在[]运算符上返回引用

我目前有一个数据库类,其中包含一些包含数据库变量的无序映射.我编写了添加和删除变量的函数,但现在想要一种方法来轻松访问和修改它们.

我的功能是这样的:

template<typename T>
T &get(const  std::string &name){
    if(typeid(T) == typeid(int))
        return this->ints[name];
    else if(typeid(T) == typeid(float))
        return this->floats[name];

     ... ect ...
}
Run Code Online (Sandbox Code Playgroud)

如果给出了无效类型,则抛出错误.

ints是类型std::unordered_map<std::string, int> 并且floats同样定义.

对我来说,这看起来是正确的.但是当我尝试运行它时,我收到以下错误:

database.hpp:98: error: invalid initialization of non-const reference of type 'int&' from an rvalue of type 'int'
Run Code Online (Sandbox Code Playgroud)

看起来无序地图没有返回引用,问题是什么?

c++ unordered-map c++14

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

#define 的“声明未声明任何内容”错误

我试图定义lllong long. 但是,这没有编译并引发错误。

我在 Windows 机器上使用 VS Code。我也在使用 gcc 8.2.0 版。

这是代码 -

#include <bits/stdc++.h>

using namespace std;

#define ll long long int;

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    ll t;

    cin >> t;

    return 0;
}

Run Code Online (Sandbox Code Playgroud)

在编译这个时,我收到了这个错误 -

test.cpp: In function 'int main()':
test.cpp:5:22: error: declaration does not declare anything [-fpermissive]
 #define ll long long int;
                      ^~~
test.cpp:12:5: note: in expansion of macro 'll'
     ll t;
     ^~
test.cpp:12:8: error: 't' was not declared in this scope …
Run Code Online (Sandbox Code Playgroud)

c++ macros compiler-errors g++ c++14

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

将数字插入字符串

我有以下字符串

std::string str = "000000"
Run Code Online (Sandbox Code Playgroud)

我想从 0 到 200 运行一个循环并在字符串中插入数字。

for( int i = 0 ; i < 200; i++ )
{
    // insert number
}
Run Code Online (Sandbox Code Playgroud)

如果i = 1,则字符串应为“000001”。

如果i = 200,则字符串应为“000200”。

目前我正在将我的数字转换为字符串,然后检查它们的长度。基于此,我将数字插入字符串 iflen = 1然后替换字符串的最后一个字符。

只是想知道我是否可以对此有更完善的方法。

c++ c++11 c++14

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

标签 统计

c++ ×10

c++14 ×10

c++11 ×3

c++17 ×2

compiler-errors ×1

g++ ×1

macros ×1

qlist ×1

qmap ×1

qt ×1

rhel7 ×1

unordered-map ×1