作用域中未声明to_string和convert.str()
to_string and convert.str() not declared in scope
我在尝试将数字转换为字符串时遇到问题。目的是进行错误检查,以确保数字具有特定的长度。我尝试过同时使用to_string()
和convert.str()
函数,但在尝试编译时返回了相同的错误。
我正在使用MinGw g++进行编译,并意识到我需要告诉它我想要C++11标准,我相信我已经做到了。我的编译器代码如下:
NPP_SAVE
CD $(CURRENT_DIRECTORY)
C:MinGWbing++ -std=c++11 "$(FULL_CURRENT_PATH)" -o "$(NAME_PART).exe"
cmd /c $(NAME_PART).exe
现在假设这是正确的,我使用to_string()
的代码如下:
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
int main() {
int book_code = 0;
cout << "Please enter the four digit book code: ";
cin >> book_code;
string code = to_string(book_code);
while (!(cin >> book_code) || code.length() != 4){
cin.clear();
cin.ignore(10000, 'n');
cout << "That is not a valid code." << endl;
cout << "Please enter the four digit book code: ";
}
}
我使用convert.str()的代码如下:
int main() {
int book_code = 0;
cout << "Please enter the four digit book code: ";
cin >> book_code;
ostringstream Convert;
convert << book_code;
string code = convert.str();
while (!(cin >> book_code) || code.length() != 4){
cin.clear();
cin.ignore(10000, 'n');
cout << "That is not a valid code." << endl;
cout << "Please enter the four digit book code: ";
}
}
这两次都没有成功,都返回了
错误:未在此作用域中声明"to_string"
我是不是错过了一些显而易见的东西?
在MinGW std::to_string()
不存在的情况下,您应该声明自己的实现。
std::string to_string(int i)
{
std::stringstream ss;
ss << i;
return ss.str();
}
我建议您使用MSYS2,它更容易实现,您可以避免此类问题。
编辑:
检查double
:中的点位置
#include <iostream>
#include <sstream>
#include <string>
std::string to_str_with_dot_pos(double i, unsigned int &pos)
{
std::stringstream ss;
ss << i;
std::string result(ss.str());
pos = 0;
while (pos < result.length() && result[pos] != '.') {
pos += 1;
}
return result;
}
int main(int argc, char **argv)
{
double d(12.54);
unsigned int pos(0);
// str should be "12.54".
// pos should be 2.
std::string str = to_str_with_dot_pos(d, pos);
std::cout << "double as string: " << str << std::endl;
std::cout << "double dot position: " << pos << std::endl;
return 0;
}
代码说明(while
循环):
它获取std::string
的每个字符,并检查它是否等于.
点字符,如果该字符不等于.
,它将向pos
变量添加+1。
它返回2而不是3,因为我们从0开始计数,而不是从1开始计数。
此外,这个问题也是重复的。
检查您的MinGw版本是否支持to_string,因为上面的代码编译正确。
我推荐一种不同的长度检查方法,一种避免使用字符串的方法:
#include <iostream>
#include <cmath>
using namespace std;
int is_len(int number, int len)
{
if(pow(10, len-1) <= number && number < pow(10, len))
return true;
return false;
}
int main()
{
int number = 1000;
cout << is_len(1, 2) << endl;
cout << is_len(1005, 4) << endl;
cout << is_len(9999, 4) << endl;
cout << is_len(599, 4) << endl;
cout << is_len(1005, 5) << endl;
return 0;
}
打印:
0
1
1
0
0
相关文章:
- Python str to C++ to Python str
- 为什么无论你输入什么,这"while(cin.get(str,3))"只运行一次?
- C++:如何将 unix 时间的字符串转换为 *tm?(使用时间错误:"cannot convert 'String' to 'tm*' ")
- 字符串变量,比如说"字符串str",可以直接复制到数组中吗?
- 解释一下 for (char c : str) 的作用?
- 返回 str vs. str.substr(0,str.size()) 在 leetcode 中给了我不同的输出
- remove(str.begin(), str.end(), );无法正常工作(我正在使用视觉工作室 2012)
- Getline(cin,str)的奇怪输出
- 如何在 C++ 中将双精度数组中的值"force convert"到字符串输出中?
- 比使用 s.str().c_str() 更好的表达?
- 除以 str.length() 什么也没显示
- "Cannot convert Python object argument to type '<typename>'" - 使用 Cython 包装 C++ 类时出错
- 没有'str'成员在 GCC 和 Clang 'std::basic_ostream<char>',但 MSVC 没有问题
- 我可以得到一个字符 * 到一个 std::sregex_iterator 匹配 str() 吗?
- C6001:使用单元化内存"str[i]"
- 我一直收到错误"cannot convert 'float*' to 'float' in return"
- 而(getline(fin,str)){}即使在到达eof之后仍在处理数据
- 在给定的代码中,有人可以解释一下(int i = 0; i<len; i++)count[str[i]]++的代码片段;
- 在函数内部传递字符 *str
- 作用域中未声明to_string和convert.str()