我无法找到解决问题的方法.让我们假设我们有这样的地图的n个元素:
std::map<string, string> tmpMap;
Run Code Online (Sandbox Code Playgroud)
我们也有这个地图的迭代器:
std::map<string, string>::iterator itTmpMap = tmpMap.begin();
Run Code Online (Sandbox Code Playgroud)
我现在如何将迭代器更改为例如第四对?Compilator不允许我只是向迭代器添加一个整数.
Compilator error:
int a = 4;
itTmpMap = itTmpMap + a;
Run Code Online (Sandbox Code Playgroud) 我有一个阵列 @a = [[9, 15], [], []]
我需要使用map方法[9,15].怎么可能?
我试过以下声明,
@a.map{|array| array.collect{|element| element} if array.any?}.compact
Run Code Online (Sandbox Code Playgroud)
但是给出[[9,15]]作为输出.任何人都可以帮助我.谢谢 :)-
我一直得到这个错误,我无法解决原因
sumSquares a = map (^2) a . foldr (+) 0
Run Code Online (Sandbox Code Playgroud)
我把数字列表的正方形加起来.
我有的是:
p(array)
array.each { |c| c=c*y**z-1 ; z=z+1 }
p(array)
Run Code Online (Sandbox Code Playgroud)
该数组是:
[35, 35, 35]
Run Code Online (Sandbox Code Playgroud)
y是36,z是一个计数器,c是数组中的值.
在我得到公式之前:
[35, 35, 35]
[formula happens]
Run Code Online (Sandbox Code Playgroud)
公式后:
[35, 35, 35]
Run Code Online (Sandbox Code Playgroud) 我正在读取一个文件,我正在尝试将它放到哪里如果我在地图中找到一个对象,它会将新对象的值添加到已经在地图中找到的对象中.我不知道的是如何使用正确的地图语法来做到这一点.这就是我所拥有的:
struct ap_pair {
ap_pair(float tp, float tm) : total_price(tp), total_amount(tm) {};
ap_pair & operator+=(const ap_pair &);
float total_price;
float total_amount;
};
void APC :: compute_total ()
{
string name;
map<string, ap_pair> :: iterator my_it;
float num1, num2, num3;
while (!fs.eof() )
{
fs >> name >> num1 >> num2; //read in file
ap_pair myobj(num1, num2); //send the weight/count and per unit price ap_pair
my_it = mymap.find(name); //returns iterator
if (my_it != mymap.end())
{
// myobj+= //ERROR here. how can …Run Code Online (Sandbox Code Playgroud) 我有一个看起来像这样的二维数组:
[true,false,false]
[false,true,false]
[false,false,true]
Run Code Online (Sandbox Code Playgroud)
我希望我能代替所有的真(布尔)与值"真"(串)和所有的假与"假"
我想在结构中使用struct作为值.为什么我必须使用value_type在地图中插入一些东西?
#include <map>
struct myStruct {};
int main()
{
std::map<int,myStruct> myStructMap;
myStruct t;
myStructMap.insert(std::map<int,myStruct>::value_type(1, t)); // OK
myStructMap.insert(1,t);
// Error:
// "no instance of overloaded function 'std::map [...]' matches
// the argument list"
}
Run Code Online (Sandbox Code Playgroud) 我想将std :: map的前N个元素复制到另一个地图.我试过copy_n但是失败了.我怎样才能做到这一点?
#include <iostream>
#include <map>
#include <algorithm>
#include <iterator>
using namespace std;
int main(){
map<int,int> Map;
for ( int i=0;i<10;i++) Map[i]=i*i;
map<int,int> Map2;
std::copy_n(Map.begin(), 5, Map2.end());
return 0;
}
Run Code Online (Sandbox Code Playgroud) 我有一个关于迭代一个hashmap的问题:
Map<String, Integer> map = new HashMap<String, Integer>();
Run Code Online (Sandbox Code Playgroud)
我想迭代这个hashmap并使用以下代码打印输出键和值对:
Iterator iterator = portMap.keySet().iterator();
Run Code Online (Sandbox Code Playgroud)
但它表明我要转换portMap.keySet().iterator();为(Iterator)portMap.keySet().iterator();但是iterator()作为迭代器已经返回,为什么我需要转换它?我检查了一些其他代码,没有演员的返回值iterator(),原因是什么,以及如何修复它?
参考这个,如果我有std::map<double, Object> my_map,是以下等价物?
Object& obj = my_map[1];
Object obj = my_map[1];
Run Code Online (Sandbox Code Playgroud)
我知道operator[]通过引用返回,第一种方式,正确的方法,而不是创建副本,或者它们是相同的.如果它们是相同的,我不确定它们为什么会这样.
同样,如果我也有......
std::map<double, Object>::iterator it;
Run Code Online (Sandbox Code Playgroud)
以下是类似的吗?
Object& obj = it->second;
Object obj = it->second;
Run Code Online (Sandbox Code Playgroud)