小编Imt*_*edi的帖子

c++中的设置操作(更新现有值)

这是我的代码:

 while (it!=s.end())  //here 's' is a set of stl and 'it' is iterator of set
    {   
        *it=*it-sub;    //'sub' is an int value
        it++;
    }
Run Code Online (Sandbox Code Playgroud)

我无法更新迭代器设置的值。我想从集合的所有元素中减去一个整数值“sub”。

任何人都可以帮助我真正的问题在哪里以及实际的解决方案是什么?

这是错误消息:

error: assignment of read-only location ‘it.std::_Rb_tree_const_iterator<int>::operator*()’
   28 |             *it=*it-sub;
      |             ~~~^~~~~~~~
Run Code Online (Sandbox Code Playgroud)

c++

20
推荐指数
3
解决办法
1719
查看次数

为什么 c++ 映射值不会为相同的键更新?

这是我的代码:

map <pair<int,int> ,string> m; 
m.insert(make_pair(1,2),"imtiaz");//making key value pair
m.insert(make_pair(8,3),"moin");
m.insert(make_pair(1,2),"izm");

cout<<m[make_pari(1,2)]<<endl; //print value for key 1,2

output:
imtiaz
Run Code Online (Sandbox Code Playgroud)

我们知道如果我们在地图中为现有键插入一个值,它会更新值。这里对于同一个键 (1,2) 我插入两个值“imtiaz”和“izm”。所以,最新的值“izm”应该在这里打印.这里有什么问题?

c++ stl

4
推荐指数
2
解决办法
1214
查看次数

为什么我们不能添加整数迭代器?

我想通过迭代器打印集合的所有值。打印endl完所有值后,我想打印一个没有最后一个的值。这是我的代码:

for (set<string> :: iterator it = str_set.begin();it!=str_set.end(); it++)
{
     cout<<*it;
     if((it+1)!=str_set.end())  //here I got error ...
     cout<<endl;
}
Run Code Online (Sandbox Code Playgroud)

但是我在检查时遇到if((it+1)!=str_set.end())错误。这里有什么问题?

这是错误消息:

error: no match for ‘operator+’ (operand types are ‘std::set<std::__cxx11::basic_string<char> >::iterator’ {aka std::_Rb_tree_const_iterator<std::__cxx11::basic_string<char> >’} and ‘int’)
   92 |         if(it+1!=str_set.end())
      |            ~~^~
      |            |  |
      |            |  int
      |            std::set<std::__cxx11::basic_string<char> >::iterator {aka std::_Rb_tree_const_iterator<std::__cxx11::basic_string<char> >}
Run Code Online (Sandbox Code Playgroud)

c++ stl

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

标签 统计

c++ ×3

stl ×2