473,221 Members | 1,907 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,221 software developers and data experts.

initializing in memory vector of floats from file with no apriori knowledge of size

I need to modify the following code to handle a
vector of pairs of floats. I was just thinking of casting
my vector of floats into being a vector of pairs of floats.

Alternately, I have looked into using the instream iterator
to initialize the vector of pairs of floats.

Any thoughts would be appreciated.

given the test.txt file which contains:

1.2 3.4
3.2 4.5
8.3 8.1
3.2 1.2
3.3 8.8

and the source code z.cpp:

#include <vector>

#include <fstream>
#include <iostream>
#include <iterator>
#include <string>
#include <assert.h>

using namespace std;

ifstream file;
if ( ! file.is_open() )
cout << "could not open file" << endl;
vector<float> numbers ;
istream_iterator<float> begin(file), end ;
copy( begin, end, back_inserter(numbers) ) ;
cout << "Size of array is: " << numbers.size() << endl;
assert( file.eof() ) ;

type "make z"

then type ./z

and you should see "Size of array is: 10"

So this code reads a file with ascii text that
happens to be rows containing pairs of floats.
It then initializes an in memory vector with the
values found. It does this without knowing how
many values are in the file. By adding enough lines
with more values, you can get the message
"Size of array is: 10000". I didn't put any code in
to make sure I have enough memory.
Oct 5 '08 #1
1 1809
9,208 Expert Mod 8TB
You won't be able to make a cast like that. Besides, this is C++ and you should not be casting like it was C.

You said it yourself: pairs of floats:
Expand|Select|Wrap|Line Numbers
  1. vector<pair<float, float> > v;
So you just create a pair, put two floats in there and push onto the vector.
Oct 5 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

by: Michael B. Allen | last post by:
Coming from C and Java on *nix I'm a little out of my element messing around with CList and MSVC++ but I think my issues are largely syntactic. I have an ADT that I use called a 'varray' that can...
by: Riadh Elloumi | last post by:
Hi, I have some problems when debugging memory allocation (for both malloc and new) in a program that uses standard lists and vectors. When I add an element to the vector, the overloaded...
by: Emanuel Ziegler | last post by:
Hi, I am using the vector class from the STL. Normally, I initialize values element by element, which is very uncomfortable and sometimes impossible (e.g. when passing a constant vector to an...
by: eriwik | last post by:
I use the following structure to store filenames for one or more "sets" grouped together by a number: map<int, map<string> > fileSets; As arguments to the constructor I send a...
by: syang8 | last post by:
hi, folks, I use Kdevelop to build some scientific simulation on Linux. If I set the size of an array N = 8000, the program works fine. However, if I set the array N some number greater than...
by: jayharris | last post by:
I'm having a ton of trouble initializing a multi-dimensional array inside a constructor, largely because I don't know the size of the array until runtime. I have a class that looks like this: ...
by: mast2as | last post by:
Hi everyone I am working on some code that uses colors. Until recently this code used colors represented a tree floats (RGB format) but recently changed so colors are now defined as spectrum....
by: liuhaoran | last post by:
HI. i have a question about memory error. when i change double variable to float variable ,for example: int curGen = 0; double sum = 0; // m_iPopSize is int variable ,NewPop is a vector...
by: zacariaz | last post by:
The subject isnt very clear, but ill do my best to explain. In a class in need a bitset which size is defined by the constructor, e.g. something like this: class Example { std::bitset<?>Bs;...
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

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.