警告隐性转换失去整数精度
Warning Implicit conversion loses integer precision
我有此代码:
//getIndividualWords(..) basically splits a sentence based on a specified delimiter: here delimiter is SPACE
vector<string> getIndividualWords(string line, string delimiter)
{
int startingPosition = 0, endingPosition = 0, delimiterLength = delimiter.length();
string words;
vector<string> wordList; //stores all the words received by splitting the sentence
while ((endingPosition = line.find (delimiter, startingPosition)) != string::npos) //the loop continues till it finds SPACES in the sentence
{
words = line.substr (startingPosition, endingPosition - startingPosition);
startingPosition = endingPosition + delimiterLength;
wordList.push_back (words);
}
wordList.push_back (line.substr (startingPosition)); //inserts the words into a vector container
return wordList;
}
我得到了此警告:
隐式转换失去整数精度
所有std::string
方法都在std::string::size_type
值(而不是int
值)上操作大小和索引。size_type
通常是std::size_t
,即 unsigned 整数。将未签名的整数转换为签名整数可能会失去精度。
因此,需要将startingPosition
,endingPosition
和delimiterLength
称为std::string::size_type
,以匹配std::string
实际使用的内容。
尝试以下操作:
vector<string> getIndividualWords(const string &line, const string &delimiter)
{
string::size_type startingPosition = 0, endingPosition, delimiterLength = delimiter.length();
string words;
vector<string> wordList;
while ((endingPosition = line.find (delimiter, startingPosition)) != string::npos)
{
words = line.substr (startingPosition, endingPosition - startingPosition);
startingPosition = endingPosition + delimiterLength;
wordList.push_back (words);
}
if (startingPosition < line.length())
wordList.push_back (line.substr (startingPosition));
return wordList;
}
相关文章:
- 我可以信任表示整数的浮点或双精度来保持精度吗
- 如何打印boost多精度128位无符号整数
- C 字符串返回字符串的整数/双精度/长整型值
- 如何检测 std::vector::emplace_back 上的隐式转换损失整数精度
- 如何防止双精度值到整数的隐式转换
- 如何解决隐式转换丢失整数精度:'size_t'(又名"无符号长")到'int'警告?
- 野牛/flex:计算器将双精度值解释为整数,所以我添加了 YYSTYPE 双精度 #define 但我有多个编译错误
- 为什么输出是整数而不是双精度?
- 从字符串转换为双精度和整数 - C++
- 从双精度转换为整数的显式类型是否始终检查整数溢出?
- C++,遇到将双精度转换为整数的问题
- 用双精度除以整数
- 使用整数初始化列表初始化长双精度的向量
- 如何判断 c++ 向量中的值类型(整数或双精度)?
- 隐式转换会丢失整数精度:'long' 到 'int'
- 警告隐性转换失去整数精度
- 使用INT32的C :隐式转换失去整数精度
- iOS - 隐式转换将整数精度"size_t"(也称为"无符号长整型")丢失为&
- srand(time(null))导致编译器警告:隐式转换会丢失整数精度
- 双精度乘以整数精度