如何将一个字符串转换为三个整数
How to convert a string into three integers
我目前正在尝试将一个字符串转换为三个整数。我需要用“11”、“5”和“2”做一些数学运算。至少在这个例子中。我尝试使用 stoi() 将字符串转换为整数,但我无法执行只获取字符串一部分的部分,如下面的第二个示例所示。有一个简单的解决方案吗?我这周才开始用C++编程,所以知识不多。
#include <iostream>
#include <cmath>
#include <string>
#include <sstream>
using namespace std;
int main() {
int seconds 01 = 0;
int seconds 10 = 0; // somehow either one was beeing set to 35 by default, so I had to do
this
int minutes = 0;
string time = "11:52"; // mm:ss format
// I need to convert the string into three integers
cout << minutes << " " << seconds10 " " << seconds01;
return 0;
}
第二个例子:
string time = "01:50"; // mm:ss format
int seconds10 = stoi(time[3]);
// [Error] call of overloaded 'stoi(char&)' is ambiguous
函数:
分钟应该输出“11”
秒 10 = 5
seconds01 = 2
stoi(time[3])
将不起作用,因为这是将单个 char
传递给 stoi()
,它需要一个以 null 结尾的 char*
字符串。您可以改用 stoi(&time[3])
(因为 string
的内部缓冲区保证在 C++11 及更高版本中以 null 终止),或者更好的是 stoi(time.c_str()+3)
,例如:
string time = "01:50"; // mm:ss format
int seconds = stoi(time.c_str()+3);
int seconds10 = seconds / 10;
int seconds01 = seconds % 10;
否则,你可以通过string::find()
和string::substr()
将std::string
分割成想要的子串,然后你可以将子串转换为整数,例如:
string time = "11:52"; // mm:ss format
auto pos = time.find(':');
int minutes = stoi(time.substr(0, pos));
int seconds = stoi(time.substr(pos+1));
int seconds10 = seconds / 10;
int seconds01 = seconds % 10;
或者,您可以将 std::string
放入 std::istringstream
,然后使用 operator>>
从中提取值,例如:
string time = "11:52"; // mm:ss format
int minutes, seconds;
char colon;
istringstream iss(time);
iss >> minutes >> colon >> seconds;
int seconds10 = seconds / 10;
int seconds01 = seconds % 10;
也就是说,您也可以像这样提取 seconds10
和 seconds01
值(参见 How to convert a single char into an int),例如:
string time = "11:52"; // mm:ss format
int minutes, seconds10, seconds01;
int minutes = ...;
int seconds10 = time[3] - '0';
int seconds01 = time[4] - '0';
问题的第一部分:拆分字符串
#include <iostream>
#include <cmath>
#include <string>
using namespace std;
int main() {
string time = "11:52"; // mm:ss format
int pos = time.find(':');
int minutes = stoi(time.substr(0, pos));
int seconds = stoi(time.substr(pos, 5));
cout << minutes <<":" << seconds<< endl;
return 0;
}
你得到分钟和秒的整数。
第 2 部分:获取几十秒
一个简单的方法是拆分整数:
// seconds is 52
int seconds01 = seconds % 10; // gives 2
int seconds10 = (int)seconds / 10; // gives 5
然后您可以将您的数学应用到秒数上。
您也可以创建这样的函数来拆分字符串并将其转换为向量。
#include <iostream>
#include <vector>
#include <sstream>
#include <algorithm>
std::vector<int> to_ints(std::string const& s, char delim)
{
std::vector<int> result;
std::istringstream iss(s);
std::string token;
while(std::getline(iss, token, delim))
result.push_back(std::stoi(token));
return result;
}
int main()
{
std::string str{ "01:50:34:2341" };
auto ints = to_ints(str, ':');
for (auto number : ints)
{
std::cout << number << "\n";
}
return 0;
}
输出
1
50
34
2341
我目前正在尝试将一个字符串转换为三个整数。我需要用“11”、“5”和“2”做一些数学运算。至少在这个例子中。我尝试使用 stoi() 将字符串转换为整数,但我无法执行只获取字符串一部分的部分,如下面的第二个示例所示。有一个简单的解决方案吗?我这周才开始用C++编程,所以知识不多。
#include <iostream>
#include <cmath>
#include <string>
#include <sstream>
using namespace std;
int main() {
int seconds 01 = 0;
int seconds 10 = 0; // somehow either one was beeing set to 35 by default, so I had to do
this
int minutes = 0;
string time = "11:52"; // mm:ss format
// I need to convert the string into three integers
cout << minutes << " " << seconds10 " " << seconds01;
return 0;
}
第二个例子:
string time = "01:50"; // mm:ss format
int seconds10 = stoi(time[3]);
// [Error] call of overloaded 'stoi(char&)' is ambiguous
函数:
分钟应该输出“11”
秒 10 = 5
seconds01 = 2
stoi(time[3])
将不起作用,因为这是将单个 char
传递给 stoi()
,它需要一个以 null 结尾的 char*
字符串。您可以改用 stoi(&time[3])
(因为 string
的内部缓冲区保证在 C++11 及更高版本中以 null 终止),或者更好的是 stoi(time.c_str()+3)
,例如:
string time = "01:50"; // mm:ss format
int seconds = stoi(time.c_str()+3);
int seconds10 = seconds / 10;
int seconds01 = seconds % 10;
否则,你可以通过string::find()
和string::substr()
将std::string
分割成想要的子串,然后你可以将子串转换为整数,例如:
string time = "11:52"; // mm:ss format
auto pos = time.find(':');
int minutes = stoi(time.substr(0, pos));
int seconds = stoi(time.substr(pos+1));
int seconds10 = seconds / 10;
int seconds01 = seconds % 10;
或者,您可以将 std::string
放入 std::istringstream
,然后使用 operator>>
从中提取值,例如:
string time = "11:52"; // mm:ss format
int minutes, seconds;
char colon;
istringstream iss(time);
iss >> minutes >> colon >> seconds;
int seconds10 = seconds / 10;
int seconds01 = seconds % 10;
也就是说,您也可以像这样提取 seconds10
和 seconds01
值(参见 How to convert a single char into an int),例如:
string time = "11:52"; // mm:ss format
int minutes, seconds10, seconds01;
int minutes = ...;
int seconds10 = time[3] - '0';
int seconds01 = time[4] - '0';
问题的第一部分:拆分字符串
#include <iostream>
#include <cmath>
#include <string>
using namespace std;
int main() {
string time = "11:52"; // mm:ss format
int pos = time.find(':');
int minutes = stoi(time.substr(0, pos));
int seconds = stoi(time.substr(pos, 5));
cout << minutes <<":" << seconds<< endl;
return 0;
}
你得到分钟和秒的整数。
第 2 部分:获取几十秒
一个简单的方法是拆分整数:
// seconds is 52
int seconds01 = seconds % 10; // gives 2
int seconds10 = (int)seconds / 10; // gives 5
然后您可以将您的数学应用到秒数上。
您也可以创建这样的函数来拆分字符串并将其转换为向量。
#include <iostream>
#include <vector>
#include <sstream>
#include <algorithm>
std::vector<int> to_ints(std::string const& s, char delim)
{
std::vector<int> result;
std::istringstream iss(s);
std::string token;
while(std::getline(iss, token, delim))
result.push_back(std::stoi(token));
return result;
}
int main()
{
std::string str{ "01:50:34:2341" };
auto ints = to_ints(str, ':');
for (auto number : ints)
{
std::cout << number << "\n";
}
return 0;
}
输出
1
50
34
2341