- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我只是通过 HackerRanks 练习我的 C++,我正在做这个问题:
Problem Statement
You are given an integer N. Find the digits in this number that exactly divide N(division that leaves 0 as remainder) and display their count. For N=24, there are 2 digits − 2 & 4. Both of these digits exactly divide 24. So our answer is 2.
Note
If the same number is repeated twice at different positions, it should be counted twice, e.g., For N=122, 2 divides 122 exactly and occurs at ones' and tens' position. So for this case, our answer is 3. Division by 0 is undefined. Input Format
The first line contains T (number of test cases) followed by T lines (each containing an integer N).
Constraints 1≤T≤15 0
这是我的程序:
1 #include <cmath>
2 #include <string>
3 #include <iostream>
4 #include <cstdlib>
5 using namespace std;
6 void checkDigits(string);
7 int main (){
8 string val;
9 int count = 0, i;
10
11 cin >> i;
12 for (int j = 1; j <= i; j++){
13 cin >> val;
14 checkDigits(val);
15 }
16 return 0;
17 }
18
19 void checkDigits(string s){
20 int len, count = 0;
21 int full, digit;
22
23 len = s.length();
24 for(int i = 0; i < len; i++){
25 full = stoi(s);
26 digit = stoi(s[i]);
27 if(full % digit == 0)
28 count++;
29 }
30 cout << count;
31 }
什么会导致我的编译器给我这个错误?
hackerrank2.cpp: In function ‘void checkDigits(std::string)’: hackerrank2.cpp:26:20: error: call of overloaded ‘stoi(char&)’ is ambiguous digit = stoi(s[i]); ^ hackerrank2.cpp:26:20: note: candidates are: In file included from /usr/include/c++/4.8/string:52:0, from hackerrank2.cpp:2: /usr/include/c++/4.8/bits/basic_string.h:2823:3: note: int std::stoi(const string&, std::size_t*, int) stoi(const string& __str, size_t* __idx = 0, int __base = 10) ^ /usr/include/c++/4.8/bits/basic_string.h:2823:3: note: no known conversion for argument 1 from ‘char’ to ‘const string& {aka const std::basic_string&}’ /usr/include/c++/4.8/bits/basic_string.h:2926:3: note: int std::stoi(const wstring&, std::size_t*, int)
stoi(const wstring& __str, size_t* __idx = 0, int __base = 10) ^ /usr/include/c++/4.8/bits/basic_string.h:2926:3: note: no known conversion for argument 1 from ‘char’ to ‘const wstring& {aka const std::basic_string&}’
最佳答案
std::stoi()
适用于字符串,而非字符;没有接受 char
的 std::stoi()
重载,这就是编译器告诉您的内容。 (请注意,列出的原型(prototype)将 const std::string &
和 const std::wstring &
作为它们各自的第一个参数。char &
是不能隐式转换为其中任何一个,因此编译器无法选择它应该使用哪个重载,因为两者都不起作用。)
如果您知道该字符是一个数字,那么您可以使用这个简单的函数:
inline int ctoi(char c)
{
return c - '0';
}
(或者只是内联:digit = s[i] - '0';
)
关于c++ - 我正在尝试使用 stoi(),但编译器正在尝试给我替换并且不会编译,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27791914/
I was wondering which way is the most recommended one in modern C++ to convert a string to a numb
我有: -Windows 7/32 位上的 cygwin 1.7.25 -g++ --版本 --> g++ (GCC) 4.8.2 -libstdc++.a --> gcc-g++-4.8.2-1 尝
我已经在 my posts 之一中询问过关于 boost::lexical_cast 的替代方案的问题,在许多回复中,我有一个建议 stoi 作为可行的替代方案。 我决定对其进行测试,令我惊讶的是,此
我正在使用最新版本的 C++,我正在尝试运行以下代码。但是,它一直告诉我 stoi “未在此范围内声明”。我是 C++ 的新手,所以如果您有任何想法,请分享。 #include using name
这个问题在这里已经有了答案: Function stoi not declared (13 个答案) 关闭 8 年前。 我正在尝试使用 stoi() 将 string 转换为 int 但我收到的错误
我正在通过编写来自用户的简单输入/输出来练习我的 C++。我现在已经搜索了一个多小时如何将字符串转换为 int。每个答案都是不同的,并且需要其他东西。 我正在使用 Code::Blocks v.13.
这个问题在这里已经有了答案: cygwin g++ std::stoi "error: ‘stoi’ is not a member of ‘std (4 个答案) 关闭 6 年前。 所以这个错误已
我有这个代码,它给我错误 terminating with uncaught exception of type std::out_of_range: stoi: out of range 我已经确定
我正在尝试将字符串解析为整数,但我不确定自己做错了什么: string input; cin >> input; int s = std::stoi(input); 这不会构建并引发错误:“stoi”
我正在使用代码块,但我无法使 stoi() 函数正常工作。我阅读了有关此问题的其他问题,但无法解决。我检查了 C+11,我正在使用命名空间 std 并且我有字符串 header 。我不知道如何解决这个
我是 Java 新手,我正在寻找 Java 中 C++ 中的类似 stoi() 的函数。 我想要这样的: 如果有像“123ABC”这样的字符串,我想提取整数中的“123”并获取“A”的索引。 我一直在
正如您所知,std::sto* 函数系列的工作方式是读取字符串中的数字,直到找到非数字,如下所示: std::string pseudoNum = "123haha"; int num = std::
我尝试编写一个将数字字符串转换为整数的函数。使用g++ 9.2.0在VS代码上运行代码时,输出错误,但是在repl.it上运行时,输出正确。这是我的代码: #include #include
我对传递到 std::stoi 的整数字符串的限制感到困惑。一个整数是 32 位的,我没记错吗?在带符号的 32 位整数中,范围可以从 −2,147,483,648 到 2,147,483,647。我
我只是通过 HackerRanks 练习我的 C++,我正在做这个问题: Problem Statement You are given an integer N. Find the digits i
我有以下测试程序: #include int main(int argc, char* argv[]) { try { return std::stoi("300012723
我上周为一项作业创建了一个复数类,我们的下一个作业是将此类修改为模板类。部分赋值重载了提取运算符,最初只处理 int 时,我将整个输入行作为一个字符串,并为实部和虚部制作子字符串,然后我使用 stoi
大多数日常用例都已转换为标准库中可用的函数。但是,我想知道其中一些是如何在幕后工作的。比如 std::stoi 背后的实际代码是什么? 最佳答案 最直接的方法是调用适当的 C 函数,在本例中为 str
c++的stoi函数定义为: int stoi(const std::string& str, std::size_t* pos = 0, int base = 10); 如您所见,base 参数默认
我正在尝试使用 stoi将字符串转换为整数,但它说它没有声明。我有标准库和 包括在内,但它仍然显示 [Error] 'stoi' was not declared in this scope 代码如下
我是一名优秀的程序员,十分优秀!