Hi,
I recently have found it necessary to move from fortran to c++ for
scientific programming... I'm working on a program that needs to resize
a 2d vector of vectors within a function... This variable "tri" is an
input arg to my function using the syntax:
function(vector <vector<int> >& tri)
The problem occurs when the tri 'matrix' is resized to triple or
quadruple the originally allocated size (which I guess requires a
complete reallocation and consequently, a change in the memory address).
Just having to deal with a slew of memory allocation issues, and I
was wondering if there is a good way to resize this 2d vector other than
defining a class to contain the tri variable, and writing a resize
function within the class?
Thanks for your help!
Matt 2 3436
The issue with vectors is that they are guarenteed to have contiguous
storage. So they are not very well suited for things that will be
resized unless you know the maximum size beforehand. If you know
the size, use reserve so that resize will not trigger subsequent
memory allocations.
I would try to find something already implemented and use that. Take
a look at valarray which is part of the standard library and also
boost's multi-array to see if they serve your purposes.
mj wrote: Hi,
I recently have found it necessary to move from fortran to c++ for scientific programming... I'm working on a program that needs to resize a 2d vector of vectors within a function... This variable "tri" is an input arg to my function using the syntax:
function(vector <vector<int> >& tri)
The problem occurs when the tri 'matrix' is resized to triple or quadruple the originally allocated size (which I guess requires a complete reallocation and consequently, a change in the memory address). Just having to deal with a slew of memory allocation issues, and I was wondering if there is a good way to resize this 2d vector other than defining a class to contain the tri variable, and writing a resize function within the class?
Thanks for your help!
Matt
mj wrote: I recently have found it necessary to move from fortran to c++ for scientific programming...
Let me just pause here, and say, wow! It took you _so_ long?...
I'm working on a program that needs to resize a 2d vector of vectors within a function... This variable "tri" is an input arg to my function using the syntax:
function(vector <vector<int> >& tri)
The problem occurs when the tri 'matrix' is resized to triple or quadruple the originally allocated size (which I guess requires a complete reallocation and consequently, a change in the memory address). Just having to deal with a slew of memory allocation issues, and I was wondering if there is a good way to resize this 2d vector other than defining a class to contain the tri variable, and writing a resize function within the class?
You might consider (a) reserving the size to what it should be so
there is no need to reallocate when you resize, or (b) use some
other structure, like a single-dimentional "vector" with the two-
dimensional interface, it's easier to resize, maybe.
Generally speaking, instead of wrapping your 'tri' in a class, do
write just a stand-alone function for resizing. There is no need
to roll out your own class just for the sake or resizing. Of course
using some pre-defined matrix class (I am sure you can find a slew
of them on the 'net) might prove beneficial. But I've never heard
of matrices changing (growing) their sizes during any operation.
V
--
Please remove capital As from my address when replying by mail This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Stefan Höhne |
last post by:
Hi,
as I recon, std::vector::clear()'s semantics changed from MS VC++ 6.0 to
MS' DOT.NET - compiler.
In the 6.0 version the capacity() of the vector did not change with the
call to clear(), in DOT.NET the capacity() is reduced to 0.
|
by: luigi |
last post by:
Hi,
I am trying to speed up the perfomance of stl vector by
allocating/deallocating blocks of memory manually. one version of the
code crashes when I try to free the memory. The other version seem to
work. I would appreciate someone to comment on this.
Version 1 (crashes on deallocating)
#include <iostream>
|
by: Daniel J Watkins |
last post by:
Hi,
Some runtime memory exceptions are being exhibited with some code I've
written. Can you clarify the following with you to see if my understanding
of the principles under question are correct. I'm trying to create a data
structure that will allow me to have a dynamic number of columns and a
dynamic number of rows in each column (there may be ten rows in column one
and twenty rows in column two for example).
i. I can declare...
|
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 std::vector::erase, it calls T::operator= only three times no matter
|
by: mrbrightsidestolemymoney |
last post by:
Hi,
I'm having a problem resizing a (very big) nested vector. It's not the
most streamlined piece of code ever but I need this array to avoid
having to recalculate the same quantity millions of times!
The (relevant) snippets of code are below : since it's relevant though
L=28,T=96 (so TasteProps weighs in at a hefty
8*96*28*28*28*96=1,618,477,056 doubles )
| |
by: Gernot Frisch |
last post by:
Hi,
I want to be able to write:
class foo
{
std::vector<intm_i (64);
}
|
by: Jeff.Boeker |
last post by:
I'm learning a lesson in how I need to be more specific :)
In C++ I can resize a vector and it will allocate memory and it will
call the default constructor if necessary (or I can supply an instance
for the copy constructor).
For example:
C++
vector<classvClass;
|
by: Bram Kuijper |
last post by:
Hi all,
I am trying to resize a vector of objects (MyObj below), which contain
references to other objects (OtherObj, see below). However, apparently
somewhere in the resize operation an assignment is done of the
referenced OtherObj object (according to the compiler messages). This is
strange, since the referenced OtherObj is initialized using member
initialization lists. Anyone a clue?
thanks,
|
by: cham |
last post by:
Hi,
I am working on c++ in a linux system ( Fedora core 4 ),
kernel version - 2.6.11-1.1369_FC4
gcc version - 4.0.0 20050519 ( Red Hat 4.0.0-8 )
In my code i am creating a vector to store pointers of type structure
"SAMPLE_TABLE_STRUCT" ( size of this structure is 36 bytes ). I create
an instance of structure "SAMPLE_TABLE_STRUCT" using operator "new"
and push back into the vector,this is done inside a for loop for
|
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: 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: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
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: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |