473,322 Members | 1,259 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Retrieving the prior to the last element of a std::vector of struct ???

What is the correct syntax for the last line???

#include <vector>
#include <stdio.h>

struct TestType {
double ONE;
double TWO;
};

void main()
{
TestType Record;
std::vector<TestTypeTest;

for (double N = 111.0; N <= 122.0; N++) {
Test.push_back(Record);
Test.back().ONE = N;
Test.back().TWO = N / 3.0;
}
for (int M = 0; M < Test.size(); M++)
printf("Test[%02d].ONE--->%6.2f
Test[%02d].TWO--->%6.2f\n",
M, Test[M].ONE, M, Test[M].TWO);

printf("Test.back().ONE--->%6.2f
Test.back().TWO--->%6.2f \n",
Test.back().ONE, Test.back().TWO);

// printf("Test.back().ONE--->%6.2f\n",
(Test.back() -1).ONE);
}
Apr 15 '07 #1
3 1432
"Peter Olcott" <No****@SeeScreen.comwrote:
What is the correct syntax for the last line???
// printf("Test.back().ONE--->%6.2f\n", (Test.back() -1).ONE);
printf("Test.back().ONE--->%6.2f\n", (Test.end() - 2)->ONE);
Apr 15 '07 #2
>
void main()
int main()

>
// printf("Test.back().ONE--->%6.2f\n",
(Test.back() -1).ONE);
}

back() returns a reference to last object, you can't
do -1 to it (you're substracting from an the object
stored in the last position).

back() is equivelent to *(--end())
your second item should be
*(Test.end() -2)
This will work on any container that has random
access iterators (like vector)... alternatively
you can do
*(--(--end()))
which will work on bidirectional iterators (like list).
Of course you need to assure that there are at least
two objects exist in the container obviously.
Apr 15 '07 #3
On 15 Apr, 19:22, Ron Natalie <r...@spamcop.netwrote:
>
back() is equivelent to *(--end())
your second item should be
*(Test.end() -2)
This will work on any container that has random
access iterators (like vector)... alternatively
you can do
*(--(--end()))
which will work on bidirectional iterators (like list).
Also, *(rbegin()+1) where iterators are random
access, or *(++rbegin()) where they are not.
Of course you need to assure that there are at least
two objects exist in the container obviously.

Apr 16 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: James Brown | last post by:
Hi, I am using the std::vector class as follows: vector <myclass *> stack1; and am pushing myclass objects onto the end of the vector like so: myclass *ptr = new myclass();...
24
by: simon | last post by:
Hi, First some background. I have a structure, struct sFileData { char*sSomeString1; char*sSomeString2;
8
by: Jason Heyes | last post by:
Does the STL have a function like this one? template <typename T> void remove(std::vector<T> &v, std::vector<T>::size_type index) { std::swap(v, v.back()); v.resize(index); } Unlike...
56
by: Peter Olcott | last post by:
I am trying to refer to the same std::vector in a class by two different names, I tried a union, and I tried a reference, I can't seem to get the syntax right. Can anyone please help? Thanks
21
by: Peter Olcott | last post by:
I got the previous alias to std::vector working, and found that it takes up the space of a pointer. I want to find a way to do an alias to a std::vector that does not take up any space. Is there...
9
by: aaragon | last post by:
I am trying to create a vector of type T and everything goes fine until I try to iterate over it. For some reason, the compiler gives me an error when I declare std::vector<T>::iterator iter;...
6
by: lokchan | last post by:
i want to create a vector of pointer s.t. it can handle new and delete but also have std::vector interface can i implement by partial specialization and inherence like follow ? #include...
4
by: gallows | last post by:
I've tried to use C qsort() on an object derivate by std::vector, but it doesn't work. I've the follow structure: struct Item { std::string name; int number; }
6
by: jmsanchezdiaz | last post by:
CPP question: if i had a struct like "struct str { int a; int b };" and a vector "std::vector < str test;" and wanted to push_back a struct, would i have to define the struct, fill it, and then...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.