Everything in this program produces the correct results except the part
dealing with the RealList. The "for" loop does not output the values that
were input to the ALL[3].RealList. What is the correct syntax to for this?
Thanks
#include <stdio.h>
#include <string>
#include <vector>
union AllType {
int Int;
double Real;
std::string* Text;
std::vector<dou ble>* RealList;
};
void main()
{
std::vector<All Type> ALL;
ALL.reserve(10) ;
printf("sizeof( ALL)--->%d\n", sizeof(ALL));
printf("sizeof( AllType)--->%d\n", sizeof(AllType) );
ALL[0].Int = 31415926;
ALL[1].Real = 3.1415926;
ALL[2].Text = new std::string("He ll Oh Whirled");
ALL[3].RealList = new std::vector<dou ble>;
ALL[3].RealList->reserve(3);
ALL[3].RealList->push_back(123. 0);
ALL[3].RealList->push_back(456. 0);
ALL[3].RealList->push_back(789. 0);
printf("%d\n", ALL[0].Int);
printf("%f\n", ALL[1].Real);
printf("%s\n", ALL[2].Text->c_str());
for (unsigned int N = 0; N < ALL[3].RealList->size(); N++)
printf("%d) %f\n", N, ALL[3].RealList[N]);
} 3 1874
"Peter Olcott" <ol****@worldne t.att.net> wrote in message
news:IR******** *************@b gtnsc04-news.ops.worldn et.att.net... Everything in this program produces the correct results except the
part dealing with the RealList. The "for" loop does not output the values
that were input to the ALL[3].RealList. What is the correct syntax to for
this? Thanks
#include <stdio.h> #include <string> #include <vector>
union AllType { int Int; double Real; std::string* Text; std::vector<dou ble>* RealList; };
void main() { std::vector<All Type> ALL; ALL.reserve(10) ; printf("sizeof( ALL)--->%d\n", sizeof(ALL)); printf("sizeof( AllType)--->%d\n", sizeof(AllType) ); ALL[0].Int = 31415926; ALL[1].Real = 3.1415926; ALL[2].Text = new std::string("He ll Oh Whirled"); ALL[3].RealList = new std::vector<dou ble>; ALL[3].RealList->reserve(3); ALL[3].RealList->push_back(123. 0); ALL[3].RealList->push_back(456. 0); ALL[3].RealList->push_back(789. 0); printf("%d\n", ALL[0].Int); printf("%f\n", ALL[1].Real); printf("%s\n", ALL[2].Text->c_str()); for (unsigned int N = 0; N < ALL[3].RealList->size(); N++) printf("%d) %f\n", N, ALL[3].RealList[N]); }
The problem is that ALL[3].RealList is a pointer to a vector, rather
than the vector itself. Consequently the [] operator is performed on the
pointer rather than the vector. If you replace the for loop with this it
should work:
const std::vector<dou ble>& v = *(ALL[3].RealList);
for (unsigned int N = 0; N < v.size(); N++)
{
printf("%d) %f\n", N, v[N]);
}
--
Peter van Merkerk
peter.van.merke rk(at)dse.nl
Peter Olcott <ol****@worldne t.att.net> wrote: void main()
int main()
{ std::vector<All Type> ALL; ALL.reserve(10) ;
You want 'resize' here, not 'reserve' if you want room for 10 items
printf("sizeof( ALL)--->%d\n", sizeof(ALL));
[This is the size of the vector's private data, completely unrelated to
the number of items stored there]
ALL[1].Real = 3.1415926; ALL[2].Text = new std::string("He ll Oh Whirled"); ALL[3].RealList = new std::vector<dou ble>; ALL[3].RealList->reserve(3);
Same here.
Andre'
--
Those who desire to give up Freedom in order to gain Security, will not have,
nor do they deserve, either one. (T. Jefferson or B. Franklin or both...)
Peter> void main()
Peter> {
Peter> std::vector<All Type> ALL;
Peter> ALL.reserve(10) ;
Peter> printf("sizeof( ALL)--->%d\n", sizeof(ALL));
Peter> printf("sizeof( AllType)--->%d\n", sizeof(AllType) );
Peter> ALL[0].Int = 31415926;
Peter> ALL[1].Real = 3.1415926;
Peter> ALL[2].Text = new std::string("He ll Oh Whirled");
Peter> ALL[3].RealList = new std::vector<dou ble>;
These last four statements yield undefined behavior because ALL does
not have any elements, as you would see if you had executed
printf("ALL.siz e()--->%d\n", ALL.size()");
If you want to cause ALL to have 10 elements, you should execute
ALL.resize(10);
instead of calling reserve.
--
Andrew Koenig, ar*@acm.org This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Invalid User |
last post by:
While trying to print a none empty list, I accidentaly put an "else"
statement with a "for" instead of "if". Here is what I had:
if ( len(mylist)> 0) :
for x,y in mylist:
print x,y
else:
print "Empty list"
which was supposed to be:
|
by: lancevictor |
last post by:
I'm learning javascript, and toward that end, I am recreating a page
"my way". The original page: http://stenbergcollege.com and the one
that I'm creating: http://www3.telus.net/neustaeter/Stenberg
When I added the buttons manually, everything was hunky-dory and it
looked pretty much exactly like the original page. When I changed the
script so that it uses the for loop to insert the top row of buttons,
space seems to get inserted...
|
by: adam |
last post by:
hello
Does exist in SQL language "for" loop ? If yes, what syntax does it has ?
best wishes
Adam
|
by: Toby Newman |
last post by:
At the page:
http://www.strath.ac.uk/IT/Docs/Ccourse/subsection3_8_3.html#SECTION0008300000000000000
or
http://tinyurl.com/4ptzs
the author warns:
"The for loop is frequently used, usually where the loop will be
traversed a fixed number of times. It is very flexible, and novice
programmers should take care not to abuse the power it offers."
|
by: Robbie Hatley |
last post by:
I'm getting a bizarre error with this code:
...
case WM_COMMAND:
{
switch (LOWORD(wParam)) // switch (control ID)
{
...
case IDC_RAIN_ENABLE_SIMPLE:
{
| |
by: Fabian Vilers |
last post by:
Hi again...
I'm wondering what could be better in terms of performance between:
var my_array = new Array();
// populate array
for (index in my_array)
{
// do something with my_array
|
by: Frederick Gotham |
last post by:
Is the domestic usage of the C "for" loop inefficient when it comes to
simple incrementation? Here's a very simple program that prints out the
bit-numbers in a byte.
#include <stdio.h>
#include <limits.h>
#include <stdlib.h>
int main(void)
{
|
by: anshuman |
last post by:
Hi,
I am trying to skip a record if some conditions match in a for loop, what is the exact keyword to use that in PL/SQL.
e.g,
DECLARE
..
i NUMBER;
BEGIN
|
by: Alexnb |
last post by:
Okay, so lets say you have a list:
funList =
and you do:
for x in funList:
print x
this will print 1-5
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |