时间戳计算(只适用于特定内容的文件)

命令行输入文件路径

#include 
#include 
#include 
#include 
#include 

using namespace std;

void getNUmberOfString(string res, int &number);
void splitStringInSpace(string str);
void readLogFile(char *file, vector<vector<string>> &vec);
int sub(int num1, int num2);
vector<string> split(const string &s, const string &seperator);

int main(int argc, char **argv)
{
    if(argc<1)
    {
        cout << "Please input file path" << endl;
        return -1;
    }
    cout << "current argc is: " << argc << endl;
    for (int i = 0; i < argc;++i)
    {
        cout << "argument[" << i << "] is:" << argv[i] << endl;
    }
    vector<vector<string>> vec;
    vector<vector<int>> res;
    readLogFile(argv[1], vec);
    for (size_t i = 0; i < vec.size(); i++)
    {
        vector<int> temp;
        for (size_t j = 0; j < vec[i].size(); j++)
        {
            int number = 0;
            splitStringInSpace(vec[i][j]);
            getNUmberOfString(vec[i][j], number);
            
            temp.push_back(number);
        }
        res.push_back(temp);
        temp.clear();
    }
    vector<double> value;
    for (size_t i = 0; i < res.size(); i++)
    {
        double subVal = 0;
        for (size_t j = 0; j < res[i].size(); j += 2)
        {
            subVal += res[i][j + 1] - res[i][j];
            // cout << "sub val = " << res[i][j + 1] - res[i][j] << endl;
        }
        // cout << "#########################" << endl;
        double val = subVal / (res[i].size() / 2);
        cout << "Average val is " << val << " ms" << endl;
        // value.push_back(val);
    }

    return 0;
}

// 从字符串中取出毫秒级的时间,单位是毫秒
void getNUmberOfString(string res, int& number)
{
    vector<string> tmp = split(res, " ");
    unsigned int pos = tmp[1].rfind(':');
    if (pos != string::npos)
    {
        res = tmp[1];
        string temp = res.substr(pos + 1);
        // cout << "temp = " << temp << endl;
        pos = temp.find('.');
        if(pos != string::npos)
        {
            string s = temp.substr(0, pos);
            // cout << "s = " << endl;
            int num = atoi(s.c_str()); //单位是s
            s = temp.substr(pos + 1);
            number = num * 1000 + atoi(s.c_str()); //ms
        }
    }
    // cout << "num is " << number << endl;
}

// 从一个字符串中取出时间
void splitStringInSpace(string str)
{
    // 查找第一个空字符
    string res = "";
    int pos = str.find_first_of(" ");
    if(pos > 0)
    {
        string temp = str.substr(pos + 1);
        for(auto s : temp)
        {
            if (s == ' ')
            {
                break;
            }
            res += s;
        }
    }
    str = res;
    // cout << "str is " << str << endl;
}

// 从文件中取到所有表示时间的那行
void readLogFile(char *file, vector<vector<string>> &vec)
{
    ifstream infile;
    infile.open(file); //将文件流对象与文件连接起来
    if(!infile.is_open())
    {
        cout << "Can not open the file!" << endl;
        exit(1);
    }
    string line;
    vector<string> str;
    while (getline(infile, line)) // line中不包括每行的换行符
    {
        // cout << "current content is " << line << endl;
        if (line.empty()) //空字符串不处理
        {
            continue;
        }
        
        line.erase(0, line.find_first_not_of(" "));//去除字符串开始的空字符
        
        if(line.at(0) == '-')
        {
            vec.push_back(str);
            str.clear();
            continue;
        }
        if(line.at(0) == '#') //第一个字符是不是#
        {
            continue;
        }
        if(line.length() == 1)
        {
            continue;
        }
        str.push_back(line);
        // cout << "content is " << line << endl;
    }
    vec.push_back(str);
    infile.close(); //关闭文件
}

vector<string> split(const string &s, const string &seperator){
  vector<string> result;
  typedef string::size_type string_size;
  string_size i = 0;
  
  while(i != s.size()){
    //找到字符串中首个不等于分隔符的字母;
    int flag = 0;
    while(i != s.size() && flag == 0){
      flag = 1;
      for(string_size x = 0; x < seperator.size(); ++x)
      {
          if(s[i] == seperator[x])
          {
              ++i;
              flag = 0;
              break;
          }
      }
    }
    
    //找到又一个分隔符,将两个分隔符之间的字符串取出;
    flag = 0;
    string_size j = i;
    while(j != s.size() && flag == 0){
      for(string_size x = 0; x < seperator.size(); ++x)
      {
          if(s[j] == seperator[x])
          {
              flag = 1;
              break;
          }
      }
      if(flag == 0) 
      {
          ++j;
      }
    }
    if(i != j){
      result.push_back(s.substr(i, j-i));
      i = j;
    }
  }
  return result;
}

你可能感兴趣的:(C++,c++,visual,studio,哈希算法)