How can I fill a map with a vector? - pointers

I have the following test code:
class Person{};
shared_ptr<Person> sp1;
shared_ptr<Person> sp2;
shared_ptr<Person> sp3;
vector<shared_ptr<Person>> members = {sp1,sp2,sp3};
map<string, shared_ptr<Person>> mymap;
How can I set the vector members to the second element of mymap? I tried with for and copy .

map is an associative container, so you need to specify the key in string type to associate it to the shared_ptr<Person>. I don't know what you need, but the following loop should work:
for (size_t i = 0; i < members.size(); ++i) {
const std::string key = "sp" + std::to_string(i+1);
mymap[key] = members[i];
}

Related

Counting numbers of key in a HashMap in Java

I read the data.csv file in Java, for the simplicity I make an example for it:
data.csv:
HAIRCOLOR;NAME
brown;John
blonde;Nathan
brown;Emily
gray;William
blonde;Helen
I have a list that contains all the data called peopleList (except the 1st row).
I would like to get the number of each color occurrence
Example for output:
brown:2
blonde:2
gray:1
etc...
I am trying to make it with a HashMap structure, like:
Map number_of_colors <String, Integer> = new HashMap()<>;
for(int i = 0; i<peopleList.size(); i++){
number_of_colors.put(peopleList.get(i).getColor(), /*and what to write here to get the number of each color?*/)
}
Or am I on the wrong path?
I was sure this can be done with HashMap.
Thank you for all your answers!
Map number_of_colors <String, Integer> = new HashMap()<>;
for(int i = 0; i<peopleList.size(); i++){
if(number_of_colors.contains(peopleList.get(i).getColor())){
number_of_colors.put(peopleList.get(i).getColor(), number_of_colors.get(peopleList.get(i).getColor())+1);
}else{
number_of_colors.put(peopleList.get(i).getColor(), 1);
}
}
You can do like this.

How to add any symbols after prefix_?

is there any solution? e.g. I have data in Map with key favorites_ prefix and values _suffix (for example: favorites_jeans, favorites_suit,...,). I want to by dint of loop get that values and set in List, because of it I must give keys of map, right?
I want to know how can I get values of myMap["favorites_*"] (* - after the favorites_ any symbols).
List<String> favoritesStrings = ['favorite_name','favorite_jeans',];
Map<String,dynamic> myMap = {
favoritesStrings[0]:'0',
favoritesStrings[1]:'1',
'someKey':'2',
'anotherKey':'3',
};
favoritesStrings.forEach((favorite)=>print(myMap[favorite]));//prints 0 1
As per what I understood, you want to fetch value from map using "favorites_" + a dynamic value from list as key.
You just have to use String templates and use $ to insert suffix variable to build key dynamically:
List<String> suffixList = ["jeans", "suit", "shirt"];
for(String suffix in suffixList) {
var item = myMap["favorites_$suffix"];
// Do something with item
}
Hope it helps

Clone a List, Map or Set in Dart

Coming from a Java background: what is the recommended way to "clone" a Dart List, Map and Set?
Use of clone() in Java is tricky and questionable1,2. Effectively, clone() is a copy constructor and for that, the Dart List, Map and Set types each have a named constructor named .from() that perform a shallow copy; e.g. given these declarations
Map<String, int> numMoons, moreMoons;
numMoons = const <String,int>{ 'Mars' : 2, 'Jupiter' : 27 };
List<String> planets, morePlanets;
you can use .from() like this:
moreMoons = new Map<String,int>.from(numMoons)
..addAll({'Saturn' : 53 });
planets = new List<String>.from(numMoons.keys);
morePlanets = new List<String>.from(planets)
..add('Pluto');
Note that List.from() more generally accepts an iterator rather than just a List.
For sake of completeness, I should mention that the dart:html Node class defines a clone() method.
1 J. Bloch, "Effective Java" 2nd Ed., Item 11.
2 B. Venners, "Josh Bloch on Design: Copy Constructor versus Cloning", 2002. Referenced from here3. Quote from the article:
If you've read the item about cloning in my book, especially if you read between the lines, you will know that I think clone is deeply broken. ---J.Bloch
3 Dart Issue #6459, clone instance(object).
With the new version of dart cloning of a Map or List become quite easy.
You can try this method for making a deep clone of List and Map.
For List
List a = ['x','y', 'z'];
List b = [...a];
For Maps
Map mapA = {"a":"b"};
Map mapB = {...mapA};
For Sets
Set setA = {1,2,3,};
Set setB = {...setA};
I hope someone find this helpful.
If you are using dart > 2.3.0, You can use spread operator something like:
List<int> a = [1,2,3];
List<int> b = [...a]; // copy of a
For lists and sets, I typically use
List<String> clone = []..addAll(originalList);
The caveat, as #kzhdev mentions, is that addAll() and from()
[Do] not really make a clone. They add a reference in the new Map/List/Set.
That's usually ok with me, but I would keep it in mind.
For deep copy (clone), you can use :
Map<String, dynamic> src = {'a': 123, 'b': 456};
Map<String, dynamic> copy = json.decode(json.encode(src));
but there may be some concerns about the performance.
This solution should work:
List list1 = [1,2,3,4];
List list2 = list1.map((element)=>element).toList();
It's for a list but should work the same for a map etc, remember to add to list if its a list at the end
Map.from() only works for 1D map.
To copy multi dimensional map without reference in dart use following method
Map<keyType, valueType> copyDeepMap( Map<keyType, valueType> map )
{
Map<keyType, valueType> newMap = {};
map.forEach
(
(key, value)
{
newMap[key] =( value is Map ) ? copyDeepMap(value) : value ;
}
);
return newMap;
}
Method-1: Recommended
For cloning a multi-dimensional (nested) List or Map, use the
json.decode() and json.encode()
List newList = json.decode(json.encode(oldList));
Map newMap = json.decode(json.encode(oldList));
Method-2:
List newList = [...oldList];
Map newMap = {...oldMap}
Method-3:
List newList = List.from(oldList);
Map newMap = Map.from(oldMap);
Method-4:
List newList = List.of(oldList);
Map newMap = Map.of(oldMap);
Method-5:
List newList = List.unmodifiable(oldList);
Map newMap = Map.unmodifiable(oldMap);
For more References:
https://www.kindacode.com/article/how-to-clone-a-list-or-map-in-dart-and-flutter/
https://coflutter.com/dart-flutter-how-to-clone-copy-a-list/
Best solution for me:
List temp = {1,2,3,4}
List platforms = json.decode(json.encode(parent.platforms));
This was my solution. I hope it can help someone.
factory Product.deepCopy(Product productToCopy) => new Product(
productToCopy.id,
productToCopy.title,
productToCopy.description,
productToCopy.price,
productToCopy.imageUrl,
productToCopy.isFavorite,
);}
To copy Map<String, List> filtered;
var filteredNewCopy = filtered.map((key, value) => MapEntry(key, [...value]));
There is no 100% bullet proof way of making an exact isolated copy, but the answer from Manish Dhruw is pretty good. However, it will only work for Maps containing simple variable types and nested Maps.
To extend it to also work with other common collections, such as List and Set, and combinations of them, you could use something like the code below.
You don't really need the DeepCopyable class, but it would be useful if you want to easily make your own classes "deep-copyable" with these functions.
abstract class DeepCopyable{
T deepCopy<T>();
}
List<T> listDeepCopy<T>(List list){
List<T> newList = List<T>();
list.forEach((value) {
newList.add(
value is Map ? mapDeepCopy(value) :
value is List ? listDeepCopy(value) :
value is Set ? setDeepCopy(value) :
value is DeepCopyable ? value.deepCopy() :
value
);
});
return newList;
}
Set<T> setDeepCopy<T>(Set s){
Set<T> newSet = Set<T>();
s.forEach((value) {
newSet.add(
value is Map ? mapDeepCopy(value) :
value is List ? listDeepCopy(value) :
value is Set ? setDeepCopy(value) :
value is DeepCopyable ? value.deepCopy() :
value
);
});
return newSet;
}
Map<K,V> mapDeepCopy<K,V>(Map<K,V> map){
Map<K,V> newMap = Map<K,V>();
map.forEach((key, value){
newMap[key] =
value is Map ? mapDeepCopy(value) :
value is List ? listDeepCopy(value) :
value is Set ? setDeepCopy(value) :
value is DeepCopyable ? value.deepCopy() :
value;
});
return newMap;
}
As I mentioned, it's obviously still not 100% bullet proof - for example you will loose type information for nested collections.
List<int> a = [1,2,3];
List<int> b = a.toList(); // copy of a
Seems to work too
**Dart 2.15
This was my solution,hope it works for you
class Person {
String? name;
int? age;
Person(this.name, this.age);
factory Person.clone(Person source) {
return Person(source.name, source.age);
}
}
final personList = [
Person('Tom', 22),
Person('Jane', 25),
];
final yourCopy = personList.map((p) => Person.clone(p)).toList();
If you are working with dynamic typed data (aka sourced from JSON or built to encode as JSON) you can use this function to perform a deep copy:
cloneDeep(value) {
if (value is List<dynamic>) {
return value.map<dynamic>(
(item) => cloneDeep(item),
).toList();
} else if (value is Map) {
return value.map<String, dynamic>(
(key, item) => MapEntry<String, dynamic>(key, cloneDeep(item)));
}
return value;
}
final list = [[],[]];
final cloned = list.copyRange(0, list.length-1);
best solution is use of toMap() and fromMap() for classes.
class A {
String title;
A(Map<String, Object> map){ // fromMap() or fromJson()
title= map['title'];
}
Map<String, Object> toMap(){
final res = <String, Object>{};
res['title'] = title;
return res;
}
}
The given answer is good, but be aware of the generate constructor which is helpful if you want to "grow" a fixed length list, e.g.:
List<String> list = new List<String>(5);
int depth = 0; // a variable to track what index we're using
...
depth++;
if (list.length <= depth) {
list = new List<String>.generate(depth * 2,
(int index) => index < depth ? list[index] : null,
growable: false);
}

Flex 3: Using array item value as an objects name

If I have a list of items in an array that represent the names of modules:
var phaseNames:Array = new Array("directorsPrep", "checkIO", "pickupPhoto", "pickupPhoto", "syncing", "dailies", "pictureEdit", "soundEdit", "soundMix", "colorCorrection", "finishing");
and I want to go through each one of these and call a function within each instance of each module, how would I go about doing so. So far, I have the following:
private function changeStartViewDate(numDays:Number):void
{
startViewDate = rightDate(startViewDate.getMonth(), startViewDate.getDate() + numDays, startViewDate.getFullYear());
getDateInfo();
determineCalendarWeek();
var phaseNames:Array = new Array("directorsPrep", "checkIO", "pickupPhoto", "pickupPhoto", "syncing", "dailies", "pictureEdit", "soundEdit", "soundMix", "colorCorrection", "finishing");
for (var i:int = 0; i < wholeProject.length; i++)
{
wholeProject[i].moveProject(Number((1-2) * numDays));
}
for (i = 0; i < phaseNames.length; i++)
{
for (var j:int = 0; j < [phaseNames[i]].length; j++)
{
[phaseNames[i]].movePhase(Number((-1) * numDays));
}
}
}
But when I try to save it, I get the following error:
1084: Syntax Error: expecting identifier before dot.
It's telling me the error is on the following line:
[phaseNames[i]].movePhase(Number((-1) * numDays));
I tried doing something like the following, but it didn't work:
[phaseNames[i].movePhase(Number((-1) * numDays))];
The above attempt of trying to figure this out gave me the following error
1064: Invalid metadata.
Any help would be appreciated.
I am going to assume that:
Each value of your phaseNames array references an actual instance of some other class [and not the name of the class]
The instance defined in the phaseNames array is a child of the current class.
You should be able to do something like this:
var childName = phaseNames[0];
var myChild : myObjectType = this[childName];
// then call function
myChild.doStuff();
This approach is not dissimilar to what you have; I'm just doing it in more lines. I'm also adding the this keyword to access the child.
I bet if you tried this, directly, it would work:
this[phaseNames[i]].movePhase(Number((-1) * numDays));
I have to wonder why you haven't created an array of all the instances instead of an array of all the variables names that point to the instances.

ASP.NET StreamWriter - new line after x commas

I've got a JS array which is writing to a text file on the server using StreamWriter. This is the line that does it:
sw.WriteLine(Request.Form["seatsArray"]);
At the moment one line is being written out with the entire contents of the array on it. I want a new line to be written after every 5 commas. Example array:
BN,ST,A1,303,601,BN,ST,A2,303,621,BN,WC,A3,303,641,
Should output:
BN,ST,A1,303,601,
BN,ST,A2,303,621,
BN,WC,A3,303,641,
I know I could use a string replace but I only know how to make this output a new line after every comma, and not after a specified amount of commas.
How can I get this to happen?
Thanks!
Well, here's the simplest answer I can think of:
string[] bits = Request.Form["seatsArray"].Split(',');
for (int i = 0; i < bits.Length; i++)
{
sw.Write(bits[i]);
sw.Write(",");
if (i % 5 == 4)
{
sw.WriteLine();
}
}
It's not terribly elegant, but it'll get the job done, I believe.
You may want this afterwards to finish off the current line, if necessary:
if (bits[i].Length % 5 != 0)
{
sw.WriteLine();
}
I'm sure there are cleverer ways... but this is simple.
One question: are the values always three characters long? Because if so, you're basically just breaking the string up every 20 characters...
Something like:
var input = "BN,ST,A1,303,601,BN,ST,A2,303,621,BN,WC,A3,303,641,";
var splitted = input.Split(',');
var cols = 5;
var rows = splitted.Length / cols;
var arr = new string[rows, cols];
for (int row = 0; row < rows; row++)
for (int col = 0; col < cols; col++)
arr[row, col] = splitted[row * cols + col];
I will try find a more elegant solution. Properly with some functional-style over it.
Update: Just find out it is not actually what you needs. With this you get a 2D array with 3 rows and 5 columns.
This however will give you 3 lines. They do not have a ending ','. Do you want that? Do you always want to print it out? Or do you want to have access to the different lines?:
var splitted = input.Split(new [] { ','}, StringSplitOptions.RemoveEmptyEntries);
var lines = from item in splitted.Select((part, i) => new { part, i })
group item by item.i / 5 into g
select string.Join(",", g.Select(a => a.part));
Or by this rather large code. But I have often needed a "Chunk" method so it may be reusable. I do not know whether there is a build-in "Chunk" method - couldn't find it.
public static class LinqExtensions
{
public static IEnumerable<IList<T>> Chunks<T>(this IEnumerable<T> xs, int size)
{
int i = 0;
var curr = new List<T>();
foreach (var x in xs)
{
curr.Add(x);
if (++i % size == 0)
{
yield return curr;
curr = new List<T>();
}
}
}
}
Usage:
var lines = input.Split(',').Chunks(5).Select(list => string.Join(",", list));

Resources