Hello!
First of all, sorry if my english sucks, it's not my first language >_<
I want some help of you guys.

I got a vector of dates named ListDate, and I want to do a search on it, to find the last days the user defines, for example:
The user want to see the dates of the last 5 days, then I have to show the last 5 days in the vector, and could have the same date twice.

I have an Edit in a form named NumeroDias, NumeroDias receives the information of the days the user wants, like that:


NumeroDias = EditNumeroDias->Text.ToDouble();

NumeroDias gets the number the user typed, on this example: the number 5.

Anyone knows how I do that?
I don't know if I was very clear, I have a problem with explanations -.-
If I wasn't clear please ask me, I need some help. :(

Thanks so much ^_^

Recommended Answers

All 7 Replies

Anyone? ._.

First question:
The user can only input the number of days that they want to look back from the present date/time right?

2nd:
Show us how ListDate is declared, how the date is stored, what other data is stored in this (if any)

Actually is a little more complicated because I'm using StringGrid to show the vectors :/
And the vector ListDate is associated with others 3 vectors, ListUser, ListEvent and ListMessage, because I'm getting the infos from a log file.
I want to show the last days the user wants from the log file.

My code is like this right now:

#include <vcl.h>
#include <vector>
#pragma hdrstop
using namespace std;

vector<AnsiString> ListDate;
vector<AnsiString> ListUser;
vector<AnsiString> ListEvent;
vector<AnsiString> ListMessage;

int Index =0;
double NumeroDias;

NumeroDias = EditNumeroDias->Text.ToDouble();

//this FOR passes through the vector
for (int i = 0; i < ListDate.size(); i++)
{

/*this IF is wrong >_< I don't know how to do, I want to show the last Dates and compare the Lines of the StringGrid cell[0], where the ListDate is, to find and show the equal dates too.*/ 

    if((Indice<=NumeroDias) && (StringGrid->Cells[0][Index] == StringGrid->Cells[0][Index]))
    {
      //Those lines show the vectors on the StringGrid 
      StringGrid->Cells[0][Index] = ListDate[i];
      StringGrid->Cells[1][Index] = ListUser[i];
      StringGrid->Cells[2][Index] = ListEvent[i];
      StringGrid->Cells[3][Index] = ListMessage[i]; 
      Index++;
    }
}

I don't have Borland C++ builder, so I don't have experience with StringGrids..

A few things:

In what format are the dates stored (YYYY/MM/DD or some other format??)

Instead of using 4 vectors, you could use one vector of structs. I'm using std::strings for my example, but you get the idea)
:

#include <iostream>
#include <vector>
#include <string>

using namespace std;

struct sData
{
    string Date;
    string User;
    string Event;
    string Message;
} ;

void ShowData(vector <sData> InVec)
{
    cout << "vector has : " << InVec.size() << " records\n\n";
    for (unsigned int i = 0; i < InVec.size(); i++)
    {
        cout << "\n-------------------------------\n"
            << InVec[i].Date << '\n' << 
            InVec[i].Event << '\n' << 
            InVec[i].Message  << '\n' << 
            InVec[i].User  << 
            "\n-------------------------------\n ";
    }
}
int main( )
{
    sData TestStruct;
    vector <sData> DataVec;
   
    TestStruct.Date = "2008-19-01";
    TestStruct.Event = "something";
    TestStruct.User = "me";
    TestStruct.Message = "...";
    DataVec.push_back(TestStruct);

    TestStruct.Date = "2008-19-02";
    TestStruct.Event = "something else";
    TestStruct.User = "you";
    TestStruct.Message = ",,,,";
    DataVec.push_back(TestStruct);

    ShowData(DataVec);
    return 0;
}

As you can see, this is a lot cleaner then using a vector for each column of data. You now have a struct for each row (tuple).

Or you could even use maps. If you're interested I could write you an example as well.

Yeah, I'm using "yyyy/mm/dd" format ^ ^
The thing is I've already done lots of other methods that way, it will be tough to change :S
I tought the search by days would be easier :confused:
Can you give me an example of "map"?

Can you give me an example of "map"?

Yes I could. But on second thought, they may not be of much use to you.
Do you need the AnsiString, or can you use std::strings? Because if you can use std::strings you could do something like this:

#include <iostream>
#include <vector>
#include <string>
#include <sstream>

using namespace std;

struct sData
{
    long Date;
    string User;
    string Event;
    string Message;
} ;

void ShowData(vector <sData> InVec, long from, long to)
{
    cout << "vector has : " << InVec.size() << " records\n";
    cout << "showing from " << from << " to " << to << "\n\n";
    for (unsigned int i = 0; i < InVec.size(); i++)
    {
        if (from <= InVec[i].Date && to >= InVec[i].Date)
        {
            cout << "\n-------------------------\n"
                << InVec[i].Date << '\n' << 
                InVec[i].Event << '\n' << 
                InVec[i].Message  << '\n' << 
                InVec[i].User  << 
                "\n-------------------------\n ";
        }
    }
    cout << "\n*****************************************\n\n";
}

//Converts a std::string date ("2008-01-01") to a long (20080101)
long DateToLong(string Input)
{
    string trimmed = Input.substr(0,4) 
        + Input.substr(5,2)+Input.substr(8,2);
    istringstream conv(trimmed);
    long number = 0;
    conv >> number;
    return number;
}

int main( )
{
    sData TestStruct;
    vector <sData> DataVec;

    TestStruct.Date = DateToLong("2008-05-06");
    TestStruct.Event = "something";
    TestStruct.User = "me";
    TestStruct.Message = "...";
    DataVec.push_back(TestStruct);

    TestStruct.Date = DateToLong("2008-10-10");
    TestStruct.Event = "something else";
    TestStruct.User = "you";
    TestStruct.Message = ",,,,";
    DataVec.push_back(TestStruct);

    ShowData(DataVec, 20080101, 20081212); //show all
    ShowData(DataVec, 20080101, 20080202); //show nothing
    ShowData(DataVec, 20080606, 20081212); //show last

    cin.get();
    return 0;
}

Thanks niek_e! ^ ^ You're such a great person!
I'll try to make some changes in my code.

Thank you so much.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.