New with vectors. Pls help

So let's say we have some dates:

2010 12 25
2011 01 15
2010 11 01

is there a way to get these 3 lines into one vector , but so it would be similar to an array. What i mean is that it has the same name (myVector) but 3 rows;
Like an array myarray[3], but so that i could work with each individual line separately?
Last edited on
build a structure with like these:

1
2
3
4
5
6
struct date
{
    int date;
    int month;
    int day;
};

is just a thot ;)
i do have a structure, but later i need to sort by one parameter and doing the same thing over and over is rubish
unless you use a for loop and '{}':
1
2
3
4
5
date mydate[10];
for(int i=0; i<10; i++)
{
   mydate[i]{date1,month1,day1};
}

these is a testing code. so not tested, but maybe it's ok. you just need change that 3 values directly on same line
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <algorithm>
#include <iostream>
#include <vector>

struct date
{
    int year;
    int month;
    int day;
};

std::istream& operator>>(std::istream& in, date& d)
{
    return in >> d.year >> d.month >> d.day;
}

std::ostream& operator<<(std::ostream& out,  const date& d)
{
    return out << d.year << ' ' << d.month << ' ' << d.day;
}

int main()
{
    std::vector<date> data;
    date temp;
    while(std::cin >> temp)
        data.push_back(temp);
    for(const auto d: data)
        std::cout << d << '\n';
    std::cout << '\n';
    //Sort by year
    std::sort(data.begin(), data.end(), [](const date& lhs, const date& rhs)
                                        { return lhs.year < rhs.year; });
    for(const auto d: data)
        std::cout << d << '\n';
}


http://ideone.com/QAIqrL
Topic archived. No new replies allowed.