473,658 Members | 2,628 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

1 New Member
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_iterato r<float> begin(file), end ;
copy( begin, end, back_inserter(n umbers) ) ;
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 1830
weaknessforcats
9,208 Recognized Expert Moderator Expert
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
8689
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 return a pointer to an arbirary sized element in an array given an index and it will allocate the memory to back it if necessary: struct varray *tests = varray_new(sizeof(struct test)); struct test *t = (struct test *)varray_get(tests, 50));
1
2337
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 "delete" operator is called somewhere in the STL library and it cannot find the allocated memory. How can I avoid this problem ? Can I overload new and delete only in my code and not in the STL? You can see my problem by compiling the mem.cpp joined...
16
7197
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 inherited constructor, since I cannot create a temporary vector before calling). Is there a possibility to assign a vector in one line, like it is possible for arrays?
3
5848
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 vector<vector<string> > where each vector<string> contains the filenames of one set. The function getNumber() calculates a number from the filename. The constructor then looks like this:
15
4782
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 10000 (actually, what I need is 80000), the program has segmentation error. The intersting thing is that the positions reporting segmentation error are different if I set N to be different values. What problem is this usually? I guess must be...
4
4395
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: class MyClass { public: const int size; MyClass( const int ); };
12
2178
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. The size of the vector went from 3 (RGB) to 151 (400 nm to 700 with a sample every 2nm). The variables are using a simple Vector class defined as follow: template<typename T, int Depth> class Vector
1
1395
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 int NumToAdd = m_iPopSize - NewPop.size();
6
6395
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; public: Example(int input) { ? = input;
0
8427
marktang
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...
0
8330
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,...
0
8746
jinu1996
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...
0
8626
tracyyun
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...
1
6178
isladogs
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...
0
4175
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...
0
4334
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2749
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1737
bsmnconsultancy
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...

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.