我有一個3列的txt文件:"dd/mm/yyyy HH:MM:SS number(000.000)"
。大約有368個條目。如何在命令中刪除向量中的重複值?
我想選擇strings
,其中第三列的值是唯一的(第一次見面)。訂單很重要。
在我的代碼我在vector
(dtp
)讀取文件,然後填充每個column
在vector
(data
,time
,pressure
)。然後我刪除第3列的值,並得到this。
我的問題是如何添加第一和第二列與正確的指數,並得到this?
數據實例(冷杉的15個值):
26.07.2017 15:47:38 82.431
26.07.2017 16:47:46 83.431
26.07.2017 17:47:54 85.431
26.07.2017 18:48:02 84.431
26.07.2017 19:48:09 83.431
26.07.2017 20:48:17 83.431
26.07.2017 21:48:24 84.431
26.07.2017 22:48:32 83.431
26.07.2017 23:48:40 83.431
27.07.2017 00:48:48 84.431
27.07.2017 01:48:55 84.431
27.07.2017 02:49:03 84.431
27.07.2017 03:49:10 84.431
27.07.2017 04:49:19 84.431
27.07.2017 05:49:27 86.431
代碼:
include <iostream>
include <fstream>
include <string>
include <algorithm>
include <iterator>
include <sstream>
include <vector>
include <cstring>
include <ctime>
using namespace std;
int main()
{
const clock_t start = clock();
system("mode con cols=50 lines=1000");
setlocale(LC_ALL, "Russian");
vector<string> dtp;
vector<string> data;
vector<string> time;
vector<double> pressure;
double num(0.0);
string line, tmp1, tmp2;
int len = 368;
int f, i, j, k;
ifstream file("data.txt");
while (!getline(file, line).eof())
dtp.push_back(line);
for (string &it : dtp)
{
{
istringstream isstr(it);
isstr >> tmp1;
data.push_back(tmp1);
}
{
istringstream isstr(it);
isstr >> tmp1 >> tmp2;
time.push_back(tmp2);
}
{
istringstream isstr(it);
isstr >> tmp1 >> tmp2 >> num;
pressure.push_back(num);
}
}
f = 0;
for (i = 0; i < len; i++)
{
for (j = i + 1; j < len; j++)
{
if (pressure[i] == pressure[j])
{
for (k = j; k < (len - 1); k++)
pressure[k] = pressure[k + 1];
len--;
j--;
f = 1;
}
}
}
if (f == 1)
{
for (i = 0; i < len; i++)
cout << pressure[i] << endl;
}
const double vremya = static_cast<double>(clock() - start)/CLOCKS_PER_SEC;
cout << "Time is: " << vremya << " seconds" << endl;
system("pause");
return 0;
}
歡迎來到Stack Overflow。請包括問題中的數據示例;不是鏈接,不是截圖。 – Beta
考慮將3個值保存在一個結構中。當你讀取每個值時,將第3列添加到一個集合或一個unordered_set,如果它尚未存在,並且它拋棄了這些數據。 –
@貝塔感謝您的意見 – dfdd