相关疑难解决方法(0)

在 C++ 中每次输出不同(意外行为)

这是合并排序的代码,有时它给出正确的输出,但有时它给出一个值改变的输出。

#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)

c++ mergesort

0
推荐指数
1
解决办法
93
查看次数

奇怪的 C++ 错误,不合格的 ID,重新声明

我想创建一个项目,我有3个文件,一个test.cppsomething.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)

c++ file

0
推荐指数
1
解决办法
41
查看次数

为什么 std::set_intersection 不起作用?

我有以下代码:

#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”,为什么?

c++ stl set-intersection

0
推荐指数
1
解决办法
164
查看次数

无法找出为什么我会出现分段错误

我是 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)

c++

0
推荐指数
1
解决办法
63
查看次数

c++中map stl的排序;错误:与运算符不匹配 -

我收到一条错误消息,指出“不匹配运算符-”。当我使用 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)

图片

图片

c++ dictionary stl

0
推荐指数
1
解决办法
97
查看次数

如何修复“抛出'std::logic_error'what()的实例后调用终止:basic_string::_M_construct null not valid”异常?

我下面的代码由二叉树数据结构组成:

#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)

c++ runtime-error runtime exception

0
推荐指数
1
解决办法
1073
查看次数

我的代码有什么问题?没有得到 C++ 程序的正确输出

我没有得到 elab 问题的正确输出。这是问题:

系里有院系,你必须根据身份证号来安排院系的名单。

测试案例 1

输入

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
Run Code Online (Sandbox Code Playgroud)

这是我使用结构实现的代码:

#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)

c++ c++14

0
推荐指数
1
解决办法
182
查看次数

std::string 附加分段错误

我在解决leetcode问题https://leetcode.com/problems/largest-number/时遇到以下错误。

terminate called after throwing an instance of 'std::length_error'
  what():  basic_string::_M_create
Run Code Online (Sandbox Code Playgroud)

因此,我在本地运行了类似的代码,它给了我分段错误。(顺便说一句,当向量大小较小(例如 10)时,它可以毫无问题地运行。我已经阅读了解决方案,所以我知道有更好的方法来解决这个 leetcode 问题。我只是想知道这个分段错误的细微差别。

#include <bits/stdc++.h>
using namespace std;

int main() {
  vector<string> arr(100, "0");

  sort(arr.begin(), arr.end(), [](string &x, string &y) {
    string a = x;
    string b = y;
    a.append(y);
    b.append(x);
    int n = a.length();
    for (int idx = 0; idx < n; ++idx) {
      if (a[idx] != b[idx]) 
        return a[idx] > b[idx]; 
    }
    return true;
  });
}
Run Code Online (Sandbox Code Playgroud)

c++ string segmentation-fault

0
推荐指数
1
解决办法
325
查看次数

使用 uint64_t 存储 18 位数字,为什么此代码不能产生所需的输出

这是一个 CSES 问题,在这里命名为数字螺旋,我知道这不是执行此操作的有效方法,但它应该有效,有人可以向我解释为什么它部分不起作用,我什至尝试了与 python 相同的方法我timelimitExceeded

#include <bits/stdc++.h>
using namespace std;
int main()
{
    int T;
    cin >> T; //no of test cases
    while(T--){
        uint64_t x, y; //I also tried using long long, but the output is same
        cin >> x >> y;
        if(x%2==0 && y%2!=0 && y< x){
            cout << fixed <<((x*x)-y)+1 << "\n";
        }else if(x%2==0 && y%2!=0 && y>x){
            cout << fixed << ((y*y)-x)+1 << "\n";
        }else if(x%2==0 && y%2==0 && y<x){
            cout << fixed << …
Run Code Online (Sandbox Code Playgroud)

c++

0
推荐指数
1
解决办法
212
查看次数

将函数返回值分配给 std::vector 元素:C++14 中的行为与 C++17 中的行为不同

断言失败并且具有未定义的行为

#include<bits/stdc++.h>

const int MaxN = 1e5 + 10;

struct Node{
    long long sum;
    int left,right;
    Node() :sum(0),left(0),right(0){

    }
    Node(long long sum,int left,int right) :sum(sum),left(left),right(right){

    }
};
struct PersistentSegmentTree{
    std::vector<Node> st;
    std::vector<int> ver;
    int n;
    PersistentSegmentTree(int n) :n(n){
        ver.push_back(0);
        st.push_back(Node());
    }
    int update(int l,int r,int pos,int val,int oldId){
        std::cerr << l << " " << r << "\n";
        st.push_back(Node());
        if(l == r){
            st.back() = Node(val,0,0);
            assert((int)st.size() - 1 != 0);
            return (int)st.size() - 1;
        }
        int cur = (int)st.size() - …
Run Code Online (Sandbox Code Playgroud)

c++ vector std c++14 c++17

0
推荐指数
1
解决办法
136
查看次数