gpt4 book ai didi

c++ - 如何按顺序删除 vector 中的重复值?

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:07:57 26 4
gpt4 key购买 nike

我有一个包含 3 列的 txt 文件:"dd/mm/yyyy HH:MM:SS number(000.000)"。大约有 368 个条目。

我想选择 strings,其中第 3 列的值是唯一的(第一次相遇)。顺序很重要。

在我的代码中,我在 vector (dtp) 中读取文件,然后在 vector 中填充每个 column ( 数据时间压力)。然后我从第 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;
}

最佳答案

我认为您最好将其视为具有两列的表格:

Timestamp Pressure

然后改用它。要使用时间戳,使用 date/time library which can parse, format and order time stamps 会有所帮助.

这是它的样子。代码下方有详细解释:

#include "date/date.h"
#include <algorithm>
#include <iostream>
#include <sstream>
#include <utility>
#include <vector>

std::istringstream file
{
"26.07.2017 15:47:38 82.431\n"
"26.07.2017 16:47:46 83.431\n"
"26.07.2017 17:47:54 85.431\n"
"26.07.2017 18:48:02 84.431\n"
"26.07.2017 19:48:09 83.431\n"
"26.07.2017 20:48:17 83.431\n"
"26.07.2017 21:48:24 84.431\n"
"26.07.2017 22:48:32 83.431\n"
"26.07.2017 23:48:40 83.431\n"
"27.07.2017 00:48:48 84.431\n"
"27.07.2017 01:48:55 84.431\n"
"27.07.2017 02:49:03 84.431\n"
"27.07.2017 03:49:10 84.431\n"
"27.07.2017 04:49:19 84.431\n"
"27.07.2017 05:49:27 86.431\n"
};

int
main()
{
using record = std::pair<date::sys_seconds, double>;
std::vector<record> records;
while (file)
{
record r;
file >> date::parse(" %d.%m.%Y %T", r.first) >> r.second;
if (file.fail())
break;
records.push_back(std::move(r));
}
std::sort(records.begin(), records.end(), [](const auto& x, const auto& y)
{return x.first < y.first;});
std::stable_sort(records.begin(), records.end(),
[](const auto& x, const auto& y)
{return x.second < y.second;});
records.erase(std::unique(records.begin(), records.end(),
[](const auto& x, const auto& y)
{return x.second == y.second;}),
records.end());
std::sort(records.begin(), records.end(), [](const auto& x, const auto& y)
{return x.first < y.first;});
for (const auto& r : records)
std::cout << date::format("%d.%m.%Y %T ", r.first) << r.second << '\n';
}

为了便于展示,我将您的 data.tx 放在了进入istringstream .不要让那个细节绊倒你。 main将同样适用于 istringstreamifstream .

我正在重用 std::pair作为我的 record , 但你可以自己写 record结构,如果你愿意。无论如何,你想收集一个vector<record>从你的数据库。这就是while循环确实如此。此循环使用 Howard Hinnant's free, open-source date/time library解析时间戳,但您也可以使用其他几种解决方案。

一旦你有records从你的数据库中填充,有三个std::algorithms它将为您完成这项工作(分 4 个步骤):

  1. 排序records按时间戳。

  2. 稳定排序 records受压力。对于相等的压力,这会保留时间戳的排序顺序。

  3. 独特的等压列表。该算法将重复的压力移动到列表的后面,并将迭代器返回到列表的“新端”。然后您需要从 [new_end, old_end) 中删除所有内容.

  4. 如果您想按时间顺序查看列表,请最后按时间戳排序。

大功告成!打印出来就行了。这将输出:

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
27.07.2017 05:49:27 86.431

它匹配你想要的输出的前缀。

关于c++ - 如何按顺序删除 vector 中的重复值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46689912/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com