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<s tring> > 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:
Files::Files(ve ctor<vector<str ing> > files&)
{
// Add files to the sets
// Loop throught all sets
for (size_t i = 0; i < files.size(); ++i)
{
vector<string>: :iterator iter = files[i].begin();
vector<string>: :iterator end = files[i].end();
// Add a file to fileSets[number][set]
for(; iter != end; ++iter)
{
fileSets[getNumber(*iter )][i] = *iter;
}
}
}
The reason that I need the first (outer) map is that the number
returned from getNumber() is very large but I don't have that many
files and there can be gaps in the numbers and so on. However I really
don't need the second (inner) map, a vector would work just as well
since I know the number of sets when the constructor is called
(files.size()).
So my question is: Is it possible to somehow "initialize " the vector in
the map to have a certain number of elements? That is if I do
fileSets[######] and that element does not already exist then a new
vector<string> will be created with a specified number of elements
allocated, so that I can then index into the vector and assigns to the
specified element?
Something like this:
Files::Files(ve ctor<vector<str ing> > files&) :
fileSets(map<ve ctor<string>(fi les.size()) >)
or something like that?
--
Eriwik 3 5846 er****@student. chalmers.se wrote: I use the following structure to store filenames for one or more "sets" grouped together by a number:
map<int, map<string> > fileSets;
Can't do that. 'map' requires two arguments. So you need to do
map<int, map<string, ???> > fileSets;
or
map<int, map<???, string> > fileSets;
Or, did you mean
map<int, vector<string> > fileSets;
???
As arguments to the constructor I send a vector<vector<s tring> > 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:
Files::Files(ve ctor<vector<str ing> > files&) { // Add files to the sets // Loop throught all sets for (size_t i = 0; i < files.size(); ++i) { vector<string>: :iterator iter = files[i].begin(); vector<string>: :iterator end = files[i].end(); // Add a file to fileSets[number][set] for(; iter != end; ++iter) { fileSets[getNumber(*iter )][i] = *iter; } } }
The reason that I need the first (outer) map is that the number returned from getNumber() is very large but I don't have that many files and there can be gaps in the numbers and so on. However I really don't need the second (inner) map, a vector would work just as well since I know the number of sets when the constructor is called (files.size()).
So my question is: Is it possible to somehow "initialize " the vector in the map to have a certain number of elements?
Yes. RTFM about what constructors are available for 'std::vector'.
That is if I do fileSets[######] and that element does not already exist then a new vector<string> will be created with a specified number of elements allocated, so that I can then index into the vector and assigns to the specified element?
Something like this:
Files::Files(ve ctor<vector<str ing> > files&) : fileSets(map<ve ctor<string>(fi les.size()) >)
or something like that?
Well, not exactly like that. 'map' cannot be initialised. You can,
of course, assign a vector of a particular size to the element of the
map. Use 'insert' instead of indexing to do that.
fileSets.insert (std::make_pair (getNumber(*ite r),files[i]));
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Victor Bazarov wrote: er****@student. chalmers.se wrote: I use the following structure to store filenames for one or more "sets" grouped together by a number:
map<int, map<string> > fileSets;
Can't do that. 'map' requires two arguments. So you need to do
Sorry about that, it should be
map<int, map<int, string> > fileSets; As arguments to the constructor I send a vector<vector<s tring> > 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:
Files::Files(ve ctor<vector<str ing> > files&) { // Add files to the sets // Loop throught all sets for (size_t i = 0; i < files.size(); ++i) { vector<string>: :iterator iter = files[i].begin(); vector<string>: :iterator end = files[i].end(); // Add a file to fileSets[number][set] for(; iter != end; ++iter) { fileSets[getNumber(*iter )][i] = *iter; } } }
The reason that I need the first (outer) map is that the number returned from getNumber() is very large but I don't have that many files and there can be gaps in the numbers and so on. However I really don't need the second (inner) map, a vector would work just as well since I know the number of sets when the constructor is called (files.size()).
So my question is: Is it possible to somehow "initialize " the vector in the map to have a certain number of elements?
Yes. RTFM about what constructors are available for 'std::vector'.
That is if I do fileSets[######] and that element does not already exist then a new vector<string> will be created with a specified number of elements allocated, so that I can then index into the vector and assigns to the specified element?
Something like this:
Files::Files(ve ctor<vector<str ing> > files&) : fileSets(map<ve ctor<string>(fi les.size()) >)
or something like that?
Well, not exactly like that. 'map' cannot be initialised. You can, of course, assign a vector of a particular size to the element of the map. Use 'insert' instead of indexing to do that.
fileSets.insert (std::make_pair (getNumber(*ite r),files[i]));
This would insert the wrong pair<int, vector<string> > into fileSets,
what I would like to do is insert one file from each set (inner vector)
at each element in the outer map.
What I wanted was this: When the []-operator is used with a key that
does not exist in the map a new element is created using the default
constructor (and a reference to this element returned is then
returned). I was hoping for a way to use another constructor instead of
the default one (in this case vector<string>( files.size()) ). However
after some more research I see that this is not possible.
--
Eriwik er****@student. chalmers.se wrote: Victor Bazarov wrote: [..some assumptions, apparently wrong..]
This would insert the wrong pair<int, vector<string> > into fileSets, what I would like to do is insert one file from each set (inner vector) at each element in the outer map.
What I wanted was this: When the []-operator is used with a key that does not exist in the map a new element is created using the default constructor (and a reference to this element returned is then returned). I was hoping for a way to use another constructor instead of the default one (in this case vector<string>( files.size()) ). However after some more research I see that this is not possible.
Right. You can't hope to use something that requires and uses the
default constructor, and at the same time not to use the default (and
instead use a parameterized) constructor. Mutually exclusive, you see.
You could try, however, insert a constructed vector first, and only
then access its elements using the []. IOW, take the insertion out of
the indexing operator.
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: hrmadhu |
last post by:
Hi,
I wish to declare a vector of deque of int, which I do as follows.
#include<vector>
#include<deque>
#include<iostream>
using namespace std;
int main(int argc, char* argv)
{
|
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...
|
by: Matthias |
last post by:
Hi,
say I have a vector v1:
std::vector<SomeType> v1;
and I need a vector v2 of pointers to v1's elements:
std::vector<SomeType*> v2;
|
by: pmatos |
last post by:
Hi all,
I have a vector of vector of ints, I could use C approach by using
int but I think C++ vector<vector<int> > would be easier to manage.
So I have a function which creates and initializes the vector with the
values I need (I know these values before hand).
- What's the best way to initialize the vector<vector<int> >? Can I...
|
by: Dennis Jones |
last post by:
Hi,
I have some old code that I am refactoring to use smart pointers and have
run into a small problem. My original code looks something like this:
class WorkerThread
{
std::map<int, Handler> &HandlerMap;
public:
WorkerThread( std::map<int, Handler> &AHandlerMap )
| |
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 );
};
|
by: John |
last post by:
Is this a valid C++ program that will not crash on any machine?
#include <iostream>
using namespace std;
int main( void ) {
int i;
cin >i;
double X;
X = 1123;
|
by: fatgirl.brown |
last post by:
Hi all,
I am attempting to initialize a vector of a vector in a constructor
with some clean-looking syntax and am not sure of how to go about this.
For instance:
double weight;
vector< vector<unsigned posInd;
Table() :
|
by: Peskov Dmitry |
last post by:
class simple_class
{
int data;
public:
simple_class() {data=10;};
simple_class(int val) : data(val){}
};
int main()
{
|
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...
|
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...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it. ...
|
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...
|
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...
|
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...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert...
|
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
| |
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...
| |