📜  C++中的std :: stof

📅  最后修改于: 2021-05-30 12:02:07             🧑  作者: Mango

解析字符串,将其内容解释为浮点数,该浮点数作为float类型的值返回。

句法 :

float stof (const string&  str, size_t* idx = 0);
float stof (const wstring& str, size_t* idx = 0);

Parameters :
str : String object with the representation of a floating-point number.
idx : Pointer to an object of type size_t, whose value is set by the function
to position of the next character in str after the numerical value.
This parameter can also be a null pointer, in which case it is not used.

Return Value :
On success, the function returns the converted floating-point number as a value of type float.

以下是std :: stof的C++实现:

// CPP code to convert floating 
// type number to string
#include 
  
int main()
{
    // String to be parsed
    std::string str = "100.80";
  
    // val to store parsed floating type number
    float val = std::stof(str);
  
    // Printing parsed floating type number
    std::cout << val;
  
    return 0;
}

输出:

100.8
// CPP code to convert integer 
// type number to string
#include 
  
int main()
{
    // String to be parsed
    std::string str = "1000";
  
    // val to store parsed integer type number
    float val = std::stof(str);
  
    // Printing parsed integer type number
    std::cout << val;
  
    return 0;
}

输出:

1000
要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”