C++ 将一个向量分成 7 组

c++ breaking up a vector in groups of 7

本文关键字:向量 一个 C++      更新时间:2023-10-16

我将标准输出数据插入到一个向量中,我需要按照模式匹配将其放入 7 组(反映入站数据),以便每个元素 [0-6] 可以运行计算(即 vrecords[2] = "dog";

我要么以无限循环结束,只有前 7 组、7 组 7 循环 7 次或垃圾。我无法制作 4 组七张独特的记录。

入站数据:

198397652
2014-11-14 15:10:10
Buy
0.00517290
0.00100000
0.00100000
0.00000517
198397685
2014-11-14 15:10:13
Buy
0.00517290
0.00100000
0.00100000
0.00000517
198398295
2014-11-14 15:11:14
Buy
0.00517290
0.00100000
0.00100000
0.00000517
203440061
2014-11-21 16:13:13
Sell
0.00825550
0.00100000
0.00100000
0.00000826

预期输出:

found buy
198397652
2014-11-14 15:10:10
Buy
0.00517290
0.00100000
0.00100000
0.00000517
found buy
198397685
2014-11-14 15:10:13
Buy
0.00517290
0.00100000
0.00100000
0.00000517
found buy
198398295
2014-11-14 15:11:14
Buy
0.00517290
0.00100000
0.00100000
0.00000517
203440061
2014-11-21 16:13:13
Sell
0.00825550
0.00100000
0.00100000
0.00000826

法典:

char *data()
{
        char buff[BUFSIZ];
        FILE *fp = stdout
        char * cstr = buff;
        std::vector<std::string>::const_iterator g;
        vector<std::string> vrecords;
        while(std::fgets(buff, sizeof buff, fp) != NULL){
                //get rid of null termination from fgets
                size_t n = std::strlen( buff );
                if ( n && buff[n-1] == 'n' ) buff[n-1] = '';
                //push everything into vector
                if ( buff[0] != '' ) vrecords.push_back( buff );
        }
        int count;
        count = 0;
        for(int t = 0; t < vrecords.size(); ++t){
                auto vecbuy = std::find( vrecords.begin(), vrecords.end(), "Buy" );
                auto vecsell = std::find( vrecords.begin(), vrecords.end(), "Sell" );
                if ( vecbuy != vrecords.end() ){
                        //cout << vrecords[t] << " " << endl;
                        cout << "found buy" << endl;
                }
                if ( vecsell != vrecords.end() ){
                        cout << "found sell" << endl;
                }
                if ( count == 6){
                        for(g=vrecords.begin(); g!=vrecords.end(); ++g){
                                std::cout<<(*g)<<std::endl;
                        }
                        count = 0;
                }
                ++count;
                //cout << vrecords[t] << " " << endl;
        }
}

现有输出:输出

在这里,试试这个。它应该是您正在寻找的更干净的版本

char *data(){
        FILE *fp = stdout;
        if (fp == NULL) perror ("Error opening file");
        char buff[BUFSIZ];
        bool more = true;
        do {
                vector<string> vrecords;
                for (int t = 0; (t < 7) && (more = (fgets(buff, BUFSIZ, fp) != NULL)); ++t) {
                        size_t n = strlen(buff);
                        if (n && buff[n - 1] == 'n')
                                buff[n - 1] = '';
                        //push everything into vector
                        if (buff[0] != '')
                                vrecords.push_back(buff);
                }
                bool buy, sell;
                if ((buy = find(vrecords.begin(), vrecords.end(), "Buy") != vrecords.end())){
                        cout << "Found buy!" << endl;
                        cout << vrecords[2] << endl;
                }
                if ((sell = find(vrecords.begin(), vrecords.end(), "Sell") != vrecords.end())){
                        cout << "Found sell!" << endl;
                }
                if (buy || sell) {
                        for (auto it = vrecords.begin(); it != vrecords.end(); ++it)
                                cout << *it << " " << endl;
                        cout << endl;
                }
        } while (more);
}
抱歉,

我不精通 c++,但逻辑就在这里。您可以使用模数并存储每个块的前两个值:

#include <stdio.h>
#include <iostream>
#include <string.h>
using namespace std;
int main()
{
    FILE * pFile;
    char buff [50], value[2][50];
    int i=0, j=0;
    pFile = fopen ("myfile.txt" , "r");
    if (pFile == NULL) perror ("Error opening file");
    else {
        while ( fgets (buff , 50 , pFile) != NULL )
            if (2 > (j = i++%7)) // buff contains one of the two first items
                strcpy(value[j], buff); // these items are stored in the value array
            else if (j==2) {            // the third item
                if (!strcmp(buff, "Buyn"))
                    cout << "Buy found" << endl;
                cout << value[0]
                     << value[1]
                     << buff;
            } else // other items
                cout << buff;
        fclose (pFile);
   }
   return 0;
}

与其使用难以理解的循环,不如编写代码,使其不仅易于理解,而且在必要时易于维护和调试。

创建"事务"类型:

#include <string>
struct Transaction
{
    bool m_isbuy;  // buy or sell
    std::string trans_date;  // date
    std::string trans_number;  // number
    std::vector<std::string> trans_data;  // vector of the data
};

现在,创建一个函数,当给定记录计数时,使用数据填充事务中的字段:

void addToTransaction(Transaction& trans, const std::string& data, int nWhich)
{
    switch (nWhich)
    {
        case 1:
            trans.trans_number = data;
        break;
        case 2:
            trans.trans_date = data;
        break;
        case 3:
            trans.m_isbuy = (data == "Buy");
        break;
        default:
            trans.trans_data.push_back(data);
        break;
    }
}

请注意,switch 语句中的每个大小写都会更改传入Transaction类型中的项。

接下来,我们进入输出函数。 这很简单:

void outputInformation(const Transaction& trans)
{
    cout << "found " << (trans.m_isbuy ? "buy" : "sell") << "n";
    cout << trans.trans_number << "n";
    cout << trans.trans_date << "n";
    cout << (trans.m_isbuy ? "Buy" : "Sell") << "n";
    copy(trans.trans_data.begin(), trans.trans_data.end(), ostream_iterator<std::string>(cout, "n"));
}

基本上,我们接受一个交易,并输出交易的信息。 代码可能有点高级,但它只是输出。 如果需要,您可以将其更改为更简单的内容。

最后,main() 函数:

int main()
{
    // open the data file
    ifstream ifs("data.txt");
    if (ifs)
    {
        std::string bufLine;
        // start with new transaction
        int counter = 0;
        Transaction curTrans;
        // start loop
        while (getline(ifs, bufLine))
        {
            // change the transaction field
            addToTransaction(curTrans, bufLine, counter+1);
            ++counter;
            // check if we need to output the info
            if (counter == 7)
            {
                // output info and start all over
                outputInformation(curTrans);
                curTrans = Transaction();
                counter = 0;
            }
        }
    }
}

你看我们没有做任何事情来寻找回车。 这很简单。

1)我们打开文件。

2) 我们在 0 处启动一个计数器。 对于文件中读取的每一行,我们调用函数来更改事务数据类型中的项目。

3)当计数器达到7时,我们输出交易并清除交易以进行另一笔交易。

代码

比循环稍长,但将此代码的可读性与您编写的循环进行比较。 任何对C++略有了解的人都可以理解和遵循它。

这是我的代码的现场演示:http://ideone.com/GIUeKQ

该演示仅接受cin输入,因此它与答案中的代码略有不同。 只需将输入从cin更改为ifs,它应该可以工作。