【问题标题】:to_string and convert.str() not declared in scopeto_string 和 convert.str() 未在范围内声明
【发布时间】:2015-06-29 14:02:29
【问题描述】:

我在尝试将数字转换为字符串时遇到问题。目的是进行错误检查以确保数字具有特定长度。我曾尝试同时使用 to_string()convert.str() 函数,但在尝试编译时得到相同的错误。

我正在使用 MinGw g++ 进行编译并意识到我需要告诉它我想要 C++11 标准,我相信我已经做到了。我的编译器代码如下:

NPP_SAVE
CD $(CURRENT_DIRECTORY)
C:\MinGW\bin\g++ -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' 未在此范围内声明

我是否遗漏了一些明显的东西?

【问题讨论】:

标签: c++ c++11 tostring


【解决方案1】:

在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。

另外,这个问题是duplicate

【讨论】:

  • 让。你的建议很有效,但我有一个跟进你。假设我想将 double 转换为字符串,即 12.54,然后检查 '.' 的位置特点。显然,我会将 int i 更改为 double i。但搜索“。”使用 find() 返回一个非常大且任意的数字。有什么建议吗?
  • 我更新了答案,检查一下!我不知道该代码是否是您正在寻找的,但它有效:p
  • 如果你在#includes下面添加using namespace std;,你不需要总是使用std::
【解决方案2】:

检查您的 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-09-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多