[英]Increase efficiency - parse integers from string C++
I have a string: 我有一个字符串:
12:56:72
12:56:72
I need to get the 3 numbers ( 12 , 56 , and 72 ) individually. 我需要分别得到了3个数字(12,56,72)。
I am doing: 我在做:
int i=0,j=0,k=0,n, p[3]; // p[i] stores each ith value
char x[], b[];
x="12:34:56";
n=strlen(x);
while(i<strlen){
b[k]=x[i];
++i;
++k;
if(x[i==]':'){
p[j]=int(b);
++j;
++i;
k=0;
char temp[] = b;
b=new char[];
delete temp;
}
}
Can this be done more efficiently? 可以更有效地完成吗?
To be "more efficient", you will have to profile. 要“更高效”,您必须进行概要分析。
Here is another solution: 这是另一种解决方案:
const std::string test_data("01:23:45");
unsigned int hours;
unsigned int minutes;
unsigned int seconds;
char separator;
std::istringstream input(test_data);
// Here's the parsing part
input >> hours >> separator >> minutes >> separator >> seconds;
Whether this is "more efficient" or not, must be measured. 必须衡量这是否“更有效”。
It looks simpler and safer. 看起来更简单,更安全。
Edit 1: Method 2 编辑1:方法2
Processors don't like loops or branches, so we can try to minimize. 处理器不喜欢循环或分支,因此我们可以尝试最小化。 This optimization assumes perfect input as a string.
此优化假定完美输入为字符串。
static const char test_data[] = "01:23:45";
unsigned int hours;
unsigned int minutes;
unsigned int seconds;
char c;
unsigned int index = 0;
hours = test_data[index++] - '0';
if (test_data[index] != ':')
{
hours = hours * 10 + test_data[index++] - '0';
}
++index; // Skip ':'
minutes = test_data[index++] - '0';
if (test_data[index] != ':')
{
minutes = minutes * 10 + test_data[index++] - '0';
}
++index; // Skip ':'
seconds = test_data[index++] - '0';
if (test_data[index] != ':')
{
seconds = seconds * 10 + test_data[index++] - '0';
}
For highest optimizations, you have to make some assumptions. 为了获得最高的优化,您必须做一些假设。 Another assumption is that the character encoding is UTF8 or ASCII, eg '1' - '0' == 1.
另一个假设是字符编码为UTF8或ASCII,例如'1'-'0'== 1。
Since you are reading the data in from a file stream you can use the operator >>
of that stream to your advantage. 由于您正在从文件流中读取数据,因此可以使用该流的
operator >>
来发挥自己的优势。 When it inputs data into a int
it will keep reading the file until it hits a character that would be invalid for an int
. 当它向
int
输入数据时,它将继续读取文件,直到它遇到对int
无效的字符为止。 This meas it will read the integer part and leave the colon in the file. 这种测量将读取整数部分并将冒号保留在文件中。 Because of this behavior we can use
由于这种行为,我们可以使用
ifstream fin("filename.ext");
int a, b, c;
char colon;
while(fin >> a >> colon >> b >> colon >> c)
{
// do stuff with a, b and c
}
And this will read file line by line as long as the file has lines in the format of 12:56:72
. 只要文件中的行格式为
12:56:72
,它就会逐行读取文件。
It looks like a clock, but still, you can scan strings with sscanf: 它看起来像一个时钟,但是仍然可以使用sscanf扫描字符串:
#include <iostream>
using namespace std;
int main()
{
char myclock[] = "10:11:12";
int hours, minutes, seconds;
sscanf(myclock, "%d:%d:%d", &hours, &minutes, &seconds);
cout << hours << endl;
cout << minutes << endl;
cout << seconds << endl;
return 0;
}
You could consider using C++ functions. 您可以考虑使用C ++函数。
#include <string>
#include <iostream>
using namespace std;
int main() {
size_t i=0,j=0,k=0, p[3]; // p[i] stores each ith value
string x("12:34:56");
for (j=0; j<3; j++) {
p[j] = stoi(x.substr(k), &i);
k += i+1;
}
for (j =0; j < 3; j++)
cout << p[j] << endl;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.