Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site
Search in forums












SourceForge.net Logo
Home » U++ Library support » U++ Core » The right container
The right container [message #51177] Sat, 16 February 2019 17:54 Go to next message
koldo is currently offline  koldo
Messages: 2931
Registered: August 2008
Veteran
I wanted to know the right U++ container for this.
Data type could be int or double.

Container<Data type> dat;

dat.SetCount(300);
dat[23] = 12;
dat.FindAdd(34);

Thank you.


Best regards
Iñaki
Re: The right container [message #51178 is a reply to message #51177] Sat, 16 February 2019 20:53 Go to previous messageGo to next message
Novo is currently offline  Novo
Messages: 726
Registered: December 2006
Contributor
VectorMap<int, int>

Regards,
Novo
Re: The right container [message #51179 is a reply to message #51178] Sun, 17 February 2019 10:35 Go to previous messageGo to next message
koldo is currently offline  koldo
Messages: 2931
Registered: August 2008
Veteran
Thank you Novo. But I want to save one data, not two.

Best regards
Iñaki
Re: The right container [message #51180 is a reply to message #51179] Sun, 17 February 2019 11:00 Go to previous messageGo to next message
Novo is currently offline  Novo
Messages: 726
Registered: December 2006
Contributor
koldo wrote on Sun, 17 February 2019 04:35
Thank you Novo. But I want to save one data, not two.

In this case
dat[23] = 12;

won't work.

An one-value alternative is Index<int>, but you will be unable to write the code line above.
If your range of indices is small and sparse, you can just use regular Vector.
Another approach is to store data in a Vector and sort it, after that you will be able to use algorithms on sorted ranges.

Hope this helps.


Regards,
Novo
Re: The right container [message #51182 is a reply to message #51180] Sun, 17 February 2019 18:03 Go to previous messageGo to next message
koldo is currently offline  koldo
Messages: 2931
Registered: August 2008
Veteran
Thank you Novo. Is what I have done.
This array is filled from text files with different formats. For some formats the Vector is best and for others Index is best.
I have used Vector, adding a FindAdd() function.

template <class Range, class V>
void FindAdd(Range& r, const V& value, int from = 0)
{
	for(int i = from; i < r.GetCount(); i++)
		if(r[i] == value) 
			return;
	r.Add(value);
}


Best regards
Iñaki
Re: The right container [message #51235 is a reply to message #51180] Sun, 24 February 2019 11:28 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 11767
Registered: November 2005
Ultimate Member
Novo wrote on Sun, 17 February 2019 11:00
koldo wrote on Sun, 17 February 2019 04:35
Thank you Novo. But I want to save one data, not two.

In this case
dat[23] = 12;

won't work.

An one-value alternative is Index<int>, but you will be unable to write the code line above.
If your range of indices is small and sparse, you can just use regular Vector.
Another approach is to store data in a Vector and sort it, after that you will be able to use algorithms on sorted ranges.

Hope this helps.


dat.Set(23, 12);

Mirek
Re: The right container [message #51236 is a reply to message #51177] Sun, 24 February 2019 11:30 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 11767
Registered: November 2005
Ultimate Member
koldo wrote on Sat, 16 February 2019 17:54
I wanted to know the right U++ container for this.
Data type could be int or double.

Container<Data type> dat;

dat.SetCount(300);
dat[23] = 12;
dat.FindAdd(34);

Thank you.


I would be helpful if you have explained what is supposed to happen at each point. Esp. what you expect from SetCount and FindAdd...

(But in general, I would say use Index. That really IS Vector with search...)

Mirek
Re: The right container [message #51239 is a reply to message #51236] Sun, 24 February 2019 18:29 Go to previous message
koldo is currently offline  koldo
Messages: 2931
Registered: August 2008
Veteran
Hi Mirek

It is used in a program that reads data from different file formats. In one of them the size is known in the beginning and data is filled randomly when reading the file. In other format data is got sequentially and you do not know total size until the end. It is possible to scan the file first to get the dimension, although it is not so efficient and a little more complex.

However do not worry, files are not huge and Vector with FindAdd works adequately.


Best regards
Iñaki
Previous Topic: Why there is no Index::Add(T&&)?
Next Topic: Add compilable testcases for nontrivial problems!
Goto Forum:
  


Current Time: Mon Mar 25 21:30:27 CET 2019

Total time taken to generate the page: 0.00825 seconds