compiler-errors 相关问题

编译阶段生成的错误,通常是由于语法和/或类型无效的问题。与[runtime-error]比较。

为什么我不能从声明为返回字符串的函数返回整数?

类解决方案{ 民众: 字符串 kthLargestNumber(向量& nums, int k) { 优先队列q; for(int i=0;i class Solution { public: string kthLargestNumber(vector<string>& nums, int k) { priority_queue<int>q; for(int i=0;i<nums.size();i++) { q.push(stoi(nums[i])); } while(!q.empty()) { k--; if(k==0) { return q.top(); } q.pop(); } return 0; } }; 我试图解决 Leetcode 1985. 找出数组中第 K 个最大整数的问题。但我在第 14 行遇到编译错误 (return q.top();) 我不知道为什么会发生这种情况。 问题链接 只要看一下代码,答案就很简单了。该函数应返回 std::string,而您则返回 和 int。您现在可能想通过使用 to_string 函数并将 int 转换为 std:string 来解决问题。 但是,这是行不通的。 该解决方案应该而且必须适用于字符串。 Constraints: 1 <= k <= nums.length <= 104 1 <= nums[i].length <= 100 nums[i] consists of only digits. nums[i] will not have any leading zeros. 很明显,你不能在int中存储100位长的数字。因此,您还必须使用字符串排序。 但这会让事情变得困难,因为你需要采用所谓的自然排序。 示例:查看 2,3 和 200,然后,如果您比较字符串,3 将大于 200。这就是他们想要从您那里得到的。自然排序。 有很多可能的解决方案。这是其中之一: #include <iostream> #include <string> #include <cctype> #include <algorithm> #include <vector> using namespace std::string_literals; // FUNCTOR for Natural sort function. Limited to signed char strings struct NaturalSort { bool operator ()(const std::string& s1, const std::string& s2) { // We want to iterate over both strings const char* i1 = s1.data(); // Get a pointer to the beginning of string 1 const char* i2 = s2.data(); // Get a pointer to the beginning of string 2 // Result of comparison. 0 means equal, positive means s1>s2, negative means s1<s2 int result = 0; // As long as both characters/numbers are equal and there are still characters in the string do { // If we found a digit at the same position in both strings if (std::isdigit(*i1) and std::isdigit(*i2)) { std::size_t pos1{}, pos2{}; // This will later indicate how many digits we read // Convert the strings to numbers and compare them result = std::stoi(i1, &pos1) - std::stoi(i2, &pos2); // Set pointers to the position after the number i1 += pos1; i2 += pos2; } else { // Normal character result = (*i1 - *i2); ++i1; ++i2; } } while ((result == 0) and (*i1 != 0 or *i2 != 0)); // Let's limit the output to -1, 0 ,1 return result < 0; } }; class Solution{ public: std::string kthLargestNumber(std::vector<std::string>& nums, int k) { std::sort(nums.begin(), nums.end(), NaturalSort()); return nums[nums.size()-k]; } }; // Driver / test code int main() { std::vector<std::string> test{ "2"s,"3"s,"20"s,"30"s,"200"s,"300"s }; Solution solution{}; for (int k = 1; k <= test.size(); ++k) std::cout << "With k=" << k << " the result would be: " << solution.kthLargestNumber(test, k) << '\n'; }

回答 1 投票 0

为什么 stoi() 函数在以下代码中不起作用?

类解决方案{ 民众: 字符串 kthLargestNumber(向量& nums, int k) { 优先队列q; for(int i=0;i class Solution { public: string kthLargestNumber(vector<string>& nums, int k) { priority_queue<int>q; for(int i=0;i<nums.size();i++) { q.push(stoi(nums[i])); } while(!q.empty()) { k--; if(k==0) { return q.top(); } q.pop(); } return 0; } }; 我试图解决 Leetcode 1985. 找出数组中第 K 个最大整数的问题。但我在第 14 行遇到编译错误( return q.top(); ),我不知道为什么会发生这种情况。 问题链接 只要看一下代码,答案就很简单了。该函数应返回 std::string,而您则返回 和 int。您现在可能想通过使用 to_string 函数并将 int 转换为 std:string 来解决问题。 但是,这是行不通的。 该解决方案应该而且必须适用于字符串。 Constraints: 1 <= k <= nums.length <= 104 1 <= nums[i].length <= 100 nums[i] consists of only digits. nums[i] will not have any leading zeros. 很明显,你不能在int中存储100位长的数字。因此,您还必须使用字符串排序。 但这会让事情变得困难,因为你需要采用所谓的自然排序。 示例:查看 2,3 和 200,然后,如果您比较字符串,3 将大于 200。这就是他们想要从您那里得到的。自然排序。 有很多可能的解决方案。这是其中之一: #include <iostream> #include <string> #include <cctype> #include <algorithm> #include <vector> using namespace std::string_literals; // FUNCTOR for Natural sort function. Limited to signed char strings struct NaturalSort { bool operator ()(const std::string& s1, const std::string& s2) { // We want to iterate over both strings const char* i1 = s1.data(); // Get a pointer to the beginning of string 1 const char* i2 = s2.data(); // Get a pointer to the beginning of string 2 // Result of comparison. 0 means equal, positive means s1>s2, negative means s1<s2 int result = 0; // As long as both characters/numbers are equal and there are still characters in the string do { // If we found a digit at the same position in both strings if (std::isdigit(*i1) and std::isdigit(*i2)) { std::size_t pos1{}, pos2{}; // This will later indicate how many digits we read // Convert the strings to numbers and compare them result = std::stoi(i1, &pos1) - std::stoi(i2, &pos2); // Set pointers to the position after the number i1 += pos1; i2 += pos2; } else { // Normal character result = (*i1 - *i2); ++i1; ++i2; } } while ((result == 0) and (*i1 != 0 or *i2 != 0)); // Let's limit the output to -1, 0 ,1 return result < 0; } }; class Solution{ public: std::string kthLargestNumber(std::vector<std::string>& nums, int k) { std::sort(nums.begin(), nums.end(), NaturalSort()); return nums[nums.size()-k]; } }; // Driver / test code int main() { std::vector<std::string> test{ "2"s,"3"s,"20"s,"30"s,"200"s,"300"s }; Solution solution{}; for (int k = 1; k <= test.size(); ++k) std::cout << "With k=" << k << " the result would be: " << solution.kthLargestNumber(test, k) << '\n'; }

回答 1 投票 0

解析不起作用:在抛出“std::invalid argument”实例后调用终止

我想要一个返回 2 个整数向量的函数。输入是一个字符串。 插入的字符串的布局应始终如下所示:“COORDINATES 123 456”,带有

回答 1 投票 0

VS代码html5样板“未知单词(CssSyntaxError)stylelint(CssSyntaxError)”

在此处输入图像描述,我的 html 样板 VScode 中显示一条红线。当我将鼠标悬停在它上面时会显示此内容(未知单词(CssSyntaxError)stylelint(CssSyntaxError)。 初级水平的东西...

回答 2 投票 0

当我尝试使用 g++ 编译时出现此错误

我有一个完全正常的代码(一个 cpp 和一个 cpp 文件)和一个 makefile,但是当我尝试用 g++ 编译它时,我得到这个: g++ -std=c++11 -c subject.cpp -o subject.o 在主题包含的文件中......

回答 1 投票 0

Visual Studio 代码概念不起作用

我正在学习C++。并致力于 Visual Studio 代码。但每当我尝试使用 #include 时,它都会给我错误。就像我创建模板概念 xyz = require(T t){} 时一样

回答 1 投票 0

如何将仅限 init 的属性的值传播到封闭类?

我有一个 Wheel 类,它具有仅限 init 的属性 Spokes: 公开课轮 { 内部轮子() { } 公共 int 辐条 { 得到;在里面; } } 这对于此类的功能至关重要...

回答 1 投票 0

Qt 不允许使用不完整类型

我正在尝试用qt制作一个简单的GUI。我想要一个 ChooseModeGUI 窗口,我可以在其中选择(单击 2 个按钮中的 1 个)并选择一种模式。 我拥有的文件是: 选择模式GUI.ui 管理员G...

回答 1 投票 0

如何使用 C++ enum+switch 语句进行详尽的编码

问题: 当我没有为类型的每个可能值包含分支时,如何让编译器警告我?当我使用可能获得值的类型进行编码时,这一点尤其重要

回答 1 投票 0

显示方法未在主JAVA中编译

我不知道如何从形状类调用显示方法到Main方法。这是一个作业,我们不应该改变 Main 中写的内容,所以问题就来了......

回答 1 投票 0

GLFW 和 GLEW 的问题

当我学习 C 和 C++ 时,我开始使用 SDL2 来可视化我正在编写的程序。我使用 Visual Studio Code,因为我喜欢它的外观,而且我不想使用 IDE,我使用 com...

回答 1 投票 0

C++ 没有“splashkit.h”头文件的文件或目录

我一直在尝试修复我的一个程序的一些构建问题,并且似乎遇到了与使用头文件构建我的程序有关的错误。 每当我尝试包含“splas...

回答 1 投票 0

使用带有列表初始化的指定初始化器时,类模板参数推导失败

在初始化结构模板实例时,使用大括号 init 语法和指定的初始值设定项时,编译器(GCC 和 Clang)会出错。 这里: 模板 是...

回答 1 投票 0

Visual Studio 2015:C++ 代码分析错误 C1253,无法加载模型文件“res://mspft140.dll/300”

Visual Studio 2015 Community update 3,尝试C++代码分析功能。设置如下: 解决方案属性 > 代码分析设置 > [所有配置] [所有平台] :

回答 1 投票 0

clang-tidy 找不到头文件

我正在 bazel 项目上运行 clang-tidy。我使用看似有效的命令生成了compile_commands.json。 我正在运行,如下所示: clang-tidy -p /path/to/compile_commands.json /

回答 1 投票 0

如何解决。错误:(-215) !empty() 函数中的 detectorMultiScale

你好,我有“错误:(-215)!empty()函数中的DetectMultiScale” xml = './haarcascade_hand.xml' face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + xml) 该代码不起作用......

回答 2 投票 0

错误:与“运算符<<' (operand types are 'QTextStream' and 'const char [3]')

这是一段基于 Qt 的代码,已经有一年了,我可能在各种上下文中编译过很多很多次,而且每次我都不知道。它位于一个名为 DPolygo 的文件中...

回答 1 投票 0

命名空间 std 中的 C++ 互斥体未命名类型

我正在编写一个简单的C++程序来演示锁的使用。我正在使用代码块和 gnu gcc 编译器。 #包括 #包括 #包括 使用 nam...

回答 10 投票 0

无法在 C++ 中运行 OpenCV 的任何内容

我已经在 python 中使用 opencv 一段时间了(在 venv 中),我必须尝试 C++ 中的 opencv 来实现功能。但是,我似乎无法运行任何东西。 做了什么 我尝试安装

回答 1 投票 0

在gtkmm-3.0中,如何使用Gtk::Widget::add_tick_callback函数调用widget的on_tick函数?

我正在尝试学习如何使用gtk3。使用C语言,gtk_widget_add_tick_callback函数非常简单,gtk文档也非常容易理解。我不能为了我的生活...

回答 1 投票 0

© www.soinside.com 2019 - 2024. All rights reserved.