QT& C ++:传递'const QString'会丢弃限定符 [英] QT & C++ : Passing 'const QString' discards qualifiers

查看:401
本文介绍了QT& C ++:传递'const QString'会丢弃限定符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个在STL中运行良好的循环,但是我一直在尝试使用正确的QTL数据类型,但它给了我错误:

I wrote a loop that ran fine in STL, however I've been attempting it with the proper QTL data types and it gave me the error:

error: passing 'const QString' as 'this' argument of 'QString& QString::operator=(char)' discards qualifiers [-fpermissive]
      dic.at(i) = char(i);

我要运行的完整代码是:

The full code that I am trying to run is this:

    QVector <QString> dic ( 256 );
for(int i = 0; i < 256; i++)
{
     dic.at(i) = char(i);
}

不太确定我需要在代码中更新什么,有什么想法吗? / p>

Not quite sure what I need to update in my code, any thoughts?

推荐答案

QVector :: at const 。您想使用 dic [i]

详细信息:由于Qt的容器是写时复制的,因此-const访问器从本质上讲更加昂贵,因为它们必须首先检查是否需要复制基础数据(并在必要时进行复制)。因此,在Qt中, at(i)将比 operator [] 更快。令人遗憾的是,这与STL相反,例如, std :: vector< T> :: at(i)会进行边界检查,因此略有限制比 std :: vector< T> :: operator [](i)更昂贵。

Details: Because Qt's containers are copy-on-write, non-const accessors are intrinsically more expensive since they have to first check if they need to copy the underlying data (and copy it if necessary). For that reason, in Qt, at(i) will be faster than operator[]. Sadly, this is the opposite of the STL, for which, e.g., std::vector<T>::at(i) does bounds checking and for that reason is slightly more expensive than std::vector<T>::operator[](i).

这篇关于QT&amp; C ++:传递'const QString'会丢弃限定符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆