我正在尝试在我的集合中插入一对 int 和 string。我想要做的是使用集合实现哈希映射,我使用存储函数存储数字及其对应的字符串,然后使用检索检索字符串。请提出问题是什么,有什么方法可以更有效地完成。错误出在 store 函数中。我没有编写 main 函数,因为它只是接收输入和调用函数。
typedef pair<int, string> pairs;
pairs p;
set<pairs> s;
set<pairs>::iterator it;
int i=0;
void store(int num,string s)
{
p[i].first=num; //error is while I am using the pair to store the string
p[i].second=s;
s.insert(p[i]);
i++;
}
string retrieve(int num)
{
for(it=s.begin();it!=s.end();++it)
{
pairs m = *it;
if(m.first==num)
{
return m.second;
}
}
}
Run Code Online (Sandbox Code Playgroud)
我的错误:
error: no match for ‘operator[]’ (operand types are ‘pairs {aka std::pair<int, std::basic_string<char> >}’ and ‘int’)
p[i].first=num;
Run Code Online (Sandbox Code Playgroud) 我是c++新手,所以我想问是否可以在c++中创建一个pair数组,就像我们创建一个pair向量一样。
int n;
cin>>n;
array < pair <int,int> >v[n];
Run Code Online (Sandbox Code Playgroud)
我正在尝试制作这样的数组,但没有得到积极的结果。
std::greater当你有课std::pair的时候有用吗int?
我正在尝试创建一个成对的优先级队列,按第一个元素排序:
std::priority_queue<std::pair<double, classA>, std::vector<std::pair<double, classA>>, std::greater<std::pair<double, classA>>> priorityQueue
Run Code Online (Sandbox Code Playgroud)
但我收到一条错误消息
与“运算符<”不匹配
它暗示了 的第二个元素std::pair,它是类类型。
应用于std::greater第一个和第二个元素std::pair?
我无法解决此错误。当我搜索此错误时,我在 Google 上没有任何运气。
没有合适的构造函数可以将“int”转换为“std::pair<int, int>”
#include <utility>
using namespace std;
pair<int, int> solve(int s, int g)
{
return s % g != 0 ? (-1, -1) : (g, s - g);
}
Run Code Online (Sandbox Code Playgroud)
错误波浪线位于它正在检查的返回中的第一个s下...
s % g != 0
我不知道如何解决这个问题。在 C# 中,这会起作用。
public static (int, int) solve(int s, int g) => s % g != 0 ? (-1, -1) : (g, s - g);
Run Code Online (Sandbox Code Playgroud) 我试图查看set_intersectionC++ 中的函数是如何工作的,对于 a vector<pair>s 并编写了这段代码:
#include <iostream>
#include <vector>
#include <bits/stdc++.h>
using namespace std;
bool comp(pair<int, int> &a, pair<int, int> &b )
{
return a.first < b.first;
}
int main()
{
vector<pair<int, int>> v1;
vector<pair<int, int>> v2;
vector<pair<int, int>> res;
v1.push_back(make_pair(1,1));
v1.push_back(make_pair(2,1));
v1.push_back(make_pair(3,2));
v1.push_back(make_pair(2,2));
v1.push_back(make_pair(1,3));
v2.push_back(make_pair(1,1)); //same
v2.push_back(make_pair(2,3));
v2.push_back(make_pair(3,2)); //same
v2.push_back(make_pair(4,2));
v2.push_back(make_pair(1,3)); //same
sort(v1.begin(), v1.end(), comp);
sort(v2.begin(), v2.end(), comp);
set_intersection(v1.begin(), v1.end(), v2.begin(), v2.end(), inserter(res, res.begin()), comp);
cout << "Intersection : " << endl;
for(auto it = 0; …Run Code Online (Sandbox Code Playgroud) 为什么我的这个代码给出了segmentation fault?我无法找到我出错的地方,对我来说似乎很好。任何帮助都会很棒,谢谢。
#include<bits/stdc++.h>
using namespace std;
int main()
{
vector <pair<int,int>> v;
int arr[5]={1,2,3,4,5};
for(int i=0;i<5;i++)
{
v[i]={arr[i],i};
}
for(int i=0;i<5;i++)
{
cout<<v[i].first<<"\n";
}
}
Run Code Online (Sandbox Code Playgroud) 我有一个inMap类型为map<double, pair<int, double>>.
我试图通过这样的方式过滤这张地图copy_if:
map<double, pair<int, double>> outMap;
copy_if(inMap.begin(), inMap.end(), outMap.begin(), [](pair<double, pair<int, double>> item) {return (true) ;} // I have simplified the predicate
Run Code Online (Sandbox Code Playgroud)
但是,在编译时,我收到以下错误:
error: use of deleted function 'std::pair<const double, std::pair<int, double>>& std::pair<const double, std::pair<int, double>>::operator=(const std::pair<const double, std::pair<int, double>>&)
Run Code Online (Sandbox Code Playgroud) 假设我想要删除的对的第一个值是 2。
像这样的向量:
vector<pair<int,int>> v = {{1,2}, {2,3} , {2,4} , {5,4}};
Run Code Online (Sandbox Code Playgroud)
删除对后将变为:
v = {{1,2}, {5,4}}
Run Code Online (Sandbox Code Playgroud)
我该怎么做?它是擦除删除惯用语的某种实现吗?因为我不知道它应该如何检查该对中的第一个值。
我有一个向量对,其中包含字符串作为标题和 int 作为值。我想添加具有相似标题的所有值。我想知道是否std::accumulate有办法探索这个,或者是否std::map也可以使用。
所以我有一个成对的向量:
std::vector<std::pair<std::string, int>> list { {"a",10},{"a",20},{"a",30},{"b",5},{"c",4},{"d",10},{"a",10},{"f",11},{"d",15},{"a",20} };它应该减少到{{"a",70},{"b",5},{"c",4},{"d",25},{"f",11}}相似字符串添加其值的位置。
这是我到目前为止所拥有的,但是j当有后续的类似标题时,我的迭代器会跳过。
for (std::size_t i = 0; i < list.size(); ++i) {
for (std::size_t j = i + 1; j < list.size(); ++j) {
if (list[i].first == list[j].first) {
list[i].second += list[j].second;
list.erase(list.begin() + j);
}
}
}
Run Code Online (Sandbox Code Playgroud)
希望有所启发。谢谢你!
我有以下地图:
std::map<char, std::pair<int, int> > robots;
Run Code Online (Sandbox Code Playgroud)
如果输入满足某些条件,我将使用此函数来填充地图:
bool World::addRobot(int row, int col, char robot_name) {
// This if block checks if the desired location is a valid 1 and keeps a track of all the robots already in the grid
if (map_[row][col] == '1' && robots.find(robot_name) == robots.end()){
map_[row][col] = robot_name;
robots.insert(make_pair(robot_name, std::make_pair(row, col)));
}
else{std::cout << "Invalid input" << std::endl;}
return true;
}
Run Code Online (Sandbox Code Playgroud)
每个机器人名称(只是一个字符)都与其位置(行/列坐标)一起保存。在以下函数中,我希望能够检索给定机器人名称的位置对:
std::pair<int, int> World::getRobot(char robot_name) {
std::pair<int, int> location = robots.find(robot_name);
return location;
}
Run Code Online (Sandbox Code Playgroud)
但名称location …