我想检查地图上是否存在钥匙(本身就是一对)。
我是新来使用map的人,无法找到要检查键(一对)的函数。
#include<bits/stdc++.h>
using namespace std;
#define ll long long int
typedef pair<ll,ll> my_key_type;
typedef map<my_key_type,ll> my_map_type;
int main()
{
my_map_type ma;
my_map_type::iterator it;
ma.insert(make_pair(my_key_type(30,40),6));
it=ma.find(30,40);
if(it==ma.end())
{
cout<<"not present";
return 0;
}
cout<<"present";
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我收到以下错误-
no matching function for call to ‘std::map<std::pair<long long int, long long int>, long long int>::find(int, int)’ it=ma.find(30,40);
Run Code Online (Sandbox Code Playgroud) 这是合并排序的代码,有时它给出正确的输出,但有时它给出一个值改变的输出。
#include "bits/stdc++.h"
using namespace std;
//function to merge two array
vector<int> merging(vector<int> a,vector<int> b){
int x = (int)a.size() + (int)b.size();
vector<int> v(x);
int p = 0;
int q = 0;
for(int i=0;i<x;++i){
if((q<(int)b.size())?a[p]<b[q]:true && p<(int)a.size()){
v[i] = a[p];
p++;
}else{
v[i] = b[q];
q++;
}
}
return v;
}
//splitting the array and then merging the array
vector<int> mergeSort(vector<int> k){
int x = (int)k.size();
if(x<2){
return k;
}
vector<int> a(k.begin(),k.begin()+(x/2));
vector<int> b(k.begin()+(x/2),k.end());
return merging(mergeSort(a),mergeSort(b));
}
int main(){
vector<int> v …Run Code Online (Sandbox Code Playgroud) 我想创建一个项目,我有3个文件,一个test.cpp,something.h和一个something.cpp。他们来了:
测试.cpp:
#include <bits/stdc++.h>
#define f cin
#define g cout
#include "something.h"
using namespace std;
int main(void)
{
int x;
register(x);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
东西.h:
#ifndef __SOMETHING__H_
#define __SOMETHING__H_
#include <bits/stdc++.h>
void register(int x);
#endif
Run Code Online (Sandbox Code Playgroud)
东西.cpp:
#include "something.h"
void register(int x)
{
std::cout << x << '\n';
}
Run Code Online (Sandbox Code Playgroud)
这是我得到的错误:
In file included from test.cpp:4:0:
something.h:5:15: error: expected unqualified-id before ‘int’
void register(int x);
^~~
something.h:5:15: error: expected ‘)’ before ‘int’
test.cpp: In function ‘int …Run Code Online (Sandbox Code Playgroud) 我有以下代码:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
// your code goes here
int t;
cin>>t;
while(t--)
{
string a;
string b;
cin>>a;
cin>>b;
vector<char> v1(a.begin(),a.end());
vector<char> v2(b.begin(),b.end());
sort(v1.begin(),v1.end());
sort(v2.begin(),v2.end());
vector<char> c;
auto ls = set_intersection(v1.begin(),v1.end(),v2.begin(),v2.end(),c.begin());
cout<<"hello"<<endl;
cout<<ls-c.begin()<<endl;
cout<<c.size()<<endl;
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
在 set_intersection 线之后没有打印任何内容,甚至没有与相交线无关的“hello”,为什么?
我是 cpp 的新手,我正在尝试解决一个练习,但我遇到了分段错误。
代码的第一部分包括初始化向量的向量。(任务 1)
然后是task2。我尝试在 while cicle 之前打印 Task 2 但它从不打印。所以我猜错误出在代码的第一部分!您可以在下面找到可以编译的代码。
您可以使用的可能输入:
0 0 0 0 0
0 0 0 0 1
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
Run Code Online (Sandbox Code Playgroud)
您可以在下面找到可以编译的代码。
#include <iostream>
#include <vector>
using namespace std;
int main (){
const int matrix_size = 5;
int i =0,j=0,taskdone=0;
vector<vector<int>> mat(matrix_size, vector<int>(matrix_size));
for(i = 0;i < matrix_size; i++)
{
for(j = 0; j < matrix_size; j++)
{
cout <<"Value mat"<<mat[i][j] << "j …Run Code Online (Sandbox Code Playgroud) 我收到一条错误消息,指出“不匹配运算符-”。当我使用 sort() 函数时会发生这种情况。
#include <bits/stdc++.h>
using namespace std;
bool comp(pair<char, int> &a, pair<char, int> &b)
{
return a.first < b.first ? 1 : -1;
}
int main() {
// your code goes here
int t;
cin >> t;
while (t--)
{
string s;
cin >> s;
map<char, int> m;
for(int i = 0; i < s.size(); i++)
{
m[s[i]]++;
cout << (char)s[i];
}
cout << "hello";
sort(m.begin(), m.end(), comp);
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我下面的代码由二叉树数据结构组成:
#include <bits/stdc++.h>
#define DEFAULT_NODE_VALUE 0
using namespace std;
template <class T>
class node{
public:
T val;
node* right = 0;
node* left = 0;
node(T a):val(a){}
};
template <class T>
class tree{
public:
node<T>* root = new node<T>(DEFAULT_NODE_VALUE);
tree(T inp_val){
root->val = inp_val;
}
void inorder_traverse(node<T>* temp){
if (!temp)
return;
inorder_traverse(temp->left);
cout << temp->val << " -> ";
inorder_traverse(temp->right);
}
void inorder_traverse(){
inorder_traverse(root);
}
};
int main()
{
tree<string> my_tree("mantap");
my_tree.root->right = new node<string>("ok");
my_tree.root->left = new node<string>("haha");
my_tree.inorder_traverse(); …Run Code Online (Sandbox Code Playgroud) 我没有得到 elab 问题的正确输出。这是问题:
系里有院系,你必须根据身份证号来安排院系的名单。
测试案例 1
输入
Run Code Online (Sandbox Code Playgroud)5 Ram 101 Rahul 95 Ashwin 75 Ahamed 106 Saurav 110输出
Run Code Online (Sandbox Code Playgroud)After Sorting Name ID Ashwin 75 Rahul 95 Ram 101 Ahamed 106 Saurav 110
这是我使用结构实现的代码:
#include<bits/stdc++.h>
#include<iostream>
using namespace std;
struct faculty{
string name;
int id;
};
int main(){
int n;
struct faculty arr[n];
for(int i=0;i<n;i++){
cin>>arr[i].name;
cin>>arr[i].id;
}
cout<<"After Sorting"<<endl;
cout<<"Name ID"<<endl;
//insertion sort
for(int i=1;i<n;i++){
struct faculty key=arr[i];
int j=i-1;
while(j>=0 && arr[j].id>key.id)
{
arr[j+1]=arr[j];
j--;
}
arr[j+1]=key;
} …Run Code Online (Sandbox Code Playgroud) 我想知道如何__gnu_cxx::__normal_iterator<char*, std::__cxx11::basic_string<char> >在 C++ 中将字符迭代器转换为字符串?
#include <iostream>\n#include <string>\n#include <algorithm>\nusing std::string;\n\n#define TO_UPPER(s) (transform((s).begin(), (s).end(), (s).begin(), ::toupper))\n#define TO_LOWER(s) (transform((s).begin(), (s).end(), (s).begin(), ::tolower))\n\n\nint main() {\n string str = "Hello world!";\n\n string result = TO_LOWER(str);\n\n std::cout << result << std::endl;\n return 0;\n}\nRun Code Online (Sandbox Code Playgroud)\n错误:
\n[1/2] Building CXX object CMakeFiles/untitled.dir/main.cpp.o\nFAILED: CMakeFiles/untitled.dir/main.cpp.o \n/usr/bin/c++ -g -std=gnu++11 -MD -MT CMakeFiles/untitled.dir/main.cpp.o -MF CMakeFiles/untitled.dir/main.cpp.o.d -o CMakeFiles/untitled.dir/main.cpp.o -c /home/amir-pc/CLionProjects/untitled/main.cpp\n/home/amir-pc/CLionProjects/untitled/main.cpp: In function \xe2\x80\x98int main()\xe2\x80\x99:\n/home/amir-pc/CLionProjects/untitled/main.cpp:7:31: error: conversion from \xe2\x80\x98__gnu_cxx::__normal_iterator<char*, std::__cxx11::basic_string<char> >\xe2\x80\x99 to non-scalar type \xe2\x80\x98std::string\xe2\x80\x99 {aka \xe2\x80\x98std::__cxx11::basic_string<char>\xe2\x80\x99} requested\n 7 …Run Code Online (Sandbox Code Playgroud) 当我编写代码时它显示错误
\n#include <bits/stdc++.h>\nusing namespace std;\n \nint main()\n{\n int arr[] = {12, 3, 4, 15};\n int n = arr.size();\n \n cout << "Size of array is " << n;\n return 0;\n}\n\n\n\n\nCompilation failed due to following error(s).\nmain.cpp:7:17: error: request for member \xe2\x80\x98size\xe2\x80\x99 in \xe2\x80\x98arr\xe2\x80\x99, which is of non-class type \xe2\x80\x98int [4]\xe2\x80\x99\n\n 7 | int n = arr.size();\n | ^~~~\nRun Code Online (Sandbox Code Playgroud)\n它也不适用于我尝试过的向量数组,也无法理解问题
\n