473,326 Members | 2,102 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,326 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;

main()
{
ifstream file;
file.open("test.txt");
if ( ! file.is_open() )
{
cout << "could not open file" << endl;
exit(1);
}
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 1815
weaknessforcats
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;
  2.  
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

11
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...
1
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...
16
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...
3
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...
15
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...
4
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: ...
12
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....
1
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...
6
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;...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
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...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
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
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
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.