相关疑难解决方法(0)

为什么 main 函数和其他函数中的数组大小不同?

我对 C++ 并不陌生,但今天我发现 main 函数和其他函数中数组的大小不同。这是为什么?我想这与指针有关。

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

void func(int arr[]){
    cout<<"func size: "<<sizeof(arr)<<"\n";
}

int main(){
    int arr[5];
    cout<<sizeof(arr)<<"\n";
    func(arr);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

您可以测试此代码以查看差异。

c++ arrays function

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

如何使用模 10^9+7

我正在尝试编写自然数平方和的代码,但使用 mod 它给出了错误的答案。这里正确的方法是什么?

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

int main()
{
    int N;
    cin>>N;
    cout<< (((N) * (N+1) * (2*N+1))/6)%mod;

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

c++ math

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

将普通字符迭代器转换为字符串

我想知道如何__gnu_cxx::__normal_iterator<char*, std::__cxx11::basic_string<char> >在 C++ 中将字符迭代器转换为字符串?

\n
#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}\n
Run 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)

c++ c++11

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

如何使用 C++ 中的 size() 函数为变量指定数组的大小?

当我编写代码时它显示错误

\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      |                 ^~~~\n
Run Code Online (Sandbox Code Playgroud)\n

它也不适用于我尝试过的向量数组,也无法理解问题

\n

c++ arrays syntax compiler-errors c++17

0
推荐指数
2
解决办法
229
查看次数

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
查看次数

错误:'运营商^'不匹配

如何在字符串中按位xor?谁能帮我这个...

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

int main() {
    // your code goes here

    string a="1001";
    string b="1111";
    string c=a^b;
    cout << "c: " << c << "\n";
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

错误:'operator ^'不匹配(操作数类型是'std :: __ cxx11 :: string {aka std :: __ cxx11 :: basic_string}'和'std :: __ cxx11 :: string {aka std :: __ cxx11 :: basic_string }')string c = a ^ b;

c++ string

-1
推荐指数
1
解决办法
802
查看次数

我不确定为什么要为此得到输出?

我正在学习C ++中的一些字符串处理,并且在代码上进行了命中和跟踪,并且出人意料地获得了给定代码的输出。

#include<bits/stdc++.h>
using namespace std;
int main(){
    char str[12]={'\67','a','v','i'};
    cout<<str;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

令人惊讶的是我得到了7avi的打印。

但是,如果我将“ \ 67”替换为“ \ 68”。在Repl.it(https://repl.it/languages/cpp)上显示以下错误

#include<bits/stdc++.h>

using namespace std;

int main(){
    char str[12]={'\68','a','v','i'};
    cout<<str;
    return 0;
}



main.cpp:6:19: warning: multi-character character constant [-Wmultichar]
    char str[12]={'\68','a','v','i'};
                  ^
main.cpp:6:19: error: constant expression evaluates to 1592 which cannot
      be narrowed to type 'char' [-Wc++11-narrowing]
    char str[12]={'\68','a','v','i'};
                  ^~~~~
main.cpp:6:19: note: insert an explicit cast to silence this issue
    char str[12]={'\68','a','v','i'};
                  ^~~~~
                  static_cast<char>( )
main.cpp:6:19: warning: implicit conversion …
Run Code Online (Sandbox Code Playgroud)

c++

-1
推荐指数
1
解决办法
54
查看次数

具有多个测试用例的 getline 问题

我想打印字符串中每个单词的第一个字母。我已经使用 getline 函数来获取带空格的字符串。它适用于单个测试用例,但不适用于多个测试用例。请帮助为什么会发生这种情况,并在可能的情况下提出解决方案以获得多个测试用例的答案。

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

string firstLetterWord(string str) { 
      string result = "";  
      if(str[0]!=' ')result.push_back(str[0]); 
    for (int i=1; i<str.length(); i++) {  
        if (str[i] != ' ' && str[i-1] == ' ') { 
            result.push_back(str[i]);    
        } 
    } 
    return result; 
} 
int main() { 
   string str;
   getline(cin,str);
   cout << firstLetterWord(str); 
   return 0; 
} 
Run Code Online (Sandbox Code Playgroud)

如果我输入 't' 测试用例的数量,然后找到字符串的答案,那么代码只给出第一个测试用例的答案。

c++ string getline

-1
推荐指数
1
解决办法
1142
查看次数

为什么记忆的解决方案比正常的递归解决方案慢?

我正在实现一个函数来计算第 n 个加泰罗尼亚数字。序列的公式如下:

我注意到记忆的解决方案比正常的递归解决方案慢。这是我的代码:

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


int catalan_number_recursive(int n){
    if (n == 0) return 1;

    else{
        int ans = 0;
        for (int i = 0; i < n; i++){
            ans += catalan_number_recursive(i)*catalan_number_recursive(n - 1 - i);
        }

        return ans;
    }
}

int catalan_number_memo(int n, map<int, int> memo){
    memo[0] = memo[1] = 1;


    if (memo.count(n) != 0){
        return memo[n];
    }
    else{
        int ans = 0;
        for (int i = 0; i < n; i++){
            ans += catalan_number_memo(i, memo)*catalan_number_memo(n …
Run Code Online (Sandbox Code Playgroud)

c++ recursion memoization dynamic-programming

-1
推荐指数
1
解决办法
411
查看次数

将第 k 个元素打印到链表中的最后一个元素的算法

我正在阅读 Cracking the Coding Interview 并做练习题,但我一直坚持这个问题:

“实现一种算法来查找单向链表的第 k 个到最后一个元素。”

我的递归函数没有返回任何东西,我不知道为什么。在递归函数中,我采用了 3 个参数。K将是我们想要找出的最后一个元素的位置。Node* temp将是头节点, int i 将保留最后一个节点的元素计数。

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

class Node
{
    int data;
    Node* next;
    public:
    Node(){
        data=0;
        next=NULL;
    }

    friend class LinkedList;
};

class LinkedList
{
    public:
    Node* head;
    public:
    LinkedList(){
        head=NULL;
    }

    void append(int data){
        Node* temp= new Node();
        temp->data=data;
        temp->next=NULL;
    
        if (head==NULL){
            head=temp;
            
        }
        else{
        Node* ptr=head;
        while(ptr->next!=NULL){
            ptr=ptr->next;
        }
        ptr->next=temp;
        }

    }

    void display(){
        Node* ptr=head;
        if(head==NULL){
            cout<<"List is empty"<<endl; …
Run Code Online (Sandbox Code Playgroud)

c++ linked-list singly-linked-list

-1
推荐指数
1
解决办法
96
查看次数