Pavel
C++
you can use std::sort with your own comparator that compares your field https://en.cppreference.com/w/cpp/algorithm/sort or if you want to sort it in std::set you need to redefine operator< to compare your field (but that can have bad effect if values of your field can be the same for different structs)
Igor🇺🇦
I have a set of objects with an int field and I have problems sorting these objects according to the value of this field of int
Pass something like this as comparator [] (const auto& a, const auto& b) { return a.field < b.field ;}
Pavel
Can you provide sample input and output?
something like this (there are extra arrays for index search, but they are just to show the idea of possible data that I can use) https://ideone.com/rVa9Lm
Pavel
I want to figure out how to sort other arrays using this info
Anonymous
something like this (there are extra arrays for index search, but they are just to show the idea of possible data that I can use) https://ideone.com/rVa9Lm
sort(soaExample.begin(), soaExample.end(), [&sortedIndexes](auto lhs, auto rhs) { return sortedIndexes[lhs.field] < sortedIndexes[rhs.field] } );
Anonymous
But be sure that the comparator satisfies requirements https://en.cppreference.com/w/cpp/algorithm/sort
Anonymous
don't I need to create an iterator for that? 🤔
Oh, begin and and is for an vector you sort
Pavel
Oh, begin and and is for an vector you sort
hm, I don't get it, I think do I need to call it for each vector? then before that I need to make a map for search an index of that vector and sort, in this case it will be n*log(n) for each vector, right? I expected something O(n) since I have all the transformations that need to be done already
Anonymous
Then I don't understand what you mean
Pavel
Then I don't understand what you mean
Yes, I was not very clear, I will try to explain more detailed. I have N vectors, all the same length, filled with some data. Let's say it represents a particle system for a game, one vector contains positions of particles, another contain colors, third contains sizes That's how I can find data for the first particle size_t particleIdx = 0; Vector2D pos = soa.positions[particleIdx]; Color color = soa.colors[particleIdx]; float size = soa.sizes[particleIdx]; Now imagine that for some reason I want to sort all this struct (each array of the struct) in a way that the particles will be stored from the smallest to the largest. So in the resulting struct the vector sizes will be just sorted ascending, the other two vectors will have their elements in position corresponding to positions of items in this sizes vector. Let's say our smallest element in sizes was on position 8 previously, the new position after the sort is 0. Then in positions vector we will move the element that was on 8th position also to 0, and the same with colors vector.
Pavel
Hope become a bit more clear now
Pavel
I mean, sort comparator gets elements as far as I know, not indexes
Pavel
should I use some non-standard sort in this case, or there's a way to make the sort to work with indexes?
Anonymous
but how to use this list to sort other vectors?
need more memory and more temporary vectors.
Anonymous
instead of swapping contents of one vector, swap from all three
Anonymous
but that will have a bunch of cache misses
Pavel
but that will have a bunch of cache misses
that will be a super-rare case so I think it not that problematic but I'll think a bit more maybe will figure out some way
Oleg
hello everyone please, share information or guide about, how to use hashtable in C. thank you!
Vlad
hello everyone please, share information or guide about, how to use hashtable in C. thank you!
Step 1) write your own hash table Step 2) use it however you want
Vlad
Or just read the documentation for the library you are using lmao
Oleg
Ok, thanks Maybe something else?
Pavel
👍.
Igor🇺🇦
Igor🇺🇦
hello everyone please, share information or guide about, how to use hashtable in C. thank you!
This is an example for C. https://medium.com/@bennettbuchanan/an-introduction-to-hash-tables-in-c-b83cbf2b4cf6
Danny
hey i want to begin to learn python but i don't know were to begin can anyone help me
Asad
It is C\C++ channel.
Danny
so you mean there is no one that is multi talanted
Hanz
maybe start downloading the executable?
Anonymous
For robotic Which is better between c and c++
͔
ok
mankind_jnr
/get cbook
Georges
Hello How can i implement a function in c++ that reverse a vector containing unique_ptr<int> elements?
Georges
I want the function to work with non-copyable types
Georges
Didn’t work
Igor🇺🇦
Didn’t work
Is this what you want https://godbolt.org/z/Wq9h33 ?
Georges
Yes, i will use it thank youu
Aakash
I think I figured it out https://ideone.com/giYvMx Swaps can be done all in one loop also. I have a gut feeling that it's too simple to be true, will test it on more data later UPD: found issues, improved the solution, now it looks more realistic: https://ideone.com/gmNFCB
I think Igor/vector-tuple point is far superior. it completes in 200instr; whereas, loops reorganized (I submitted, then removed loop check links) only optimized 100 or consolidated to 150max instr. Heavy vector looping will cause cpu exhausting..
Pat
Hi
Pavel
I'm a bad reader :)
Aakash
Can you please resend the link, I can't find it
I delete that!!! As unnecessary mixup will happen.. It’s was just commenting your loop code and just reconsidering c/style loop as independent.. with simple mov key[i]=key[i].. so on for v1,v2,v3.. considering as single “mov” instr without calling swap.. Something like.. —It’s incorrect assignment— for(int i=0; i<sortedPositions.size(); i++) { soaExample.keys[i] = soaExample.keys[i]; } This helped in understanding your loop reductions! With single mov instr..
Aakash
Just consolidate you loop as one; 4-in-1 you will see it’s reducing 100+ instr.. Significant change!!
Pavel
Just consolidate you loop as one; 4-in-1 you will see it’s reducing 100+ instr.. Significant change!!
It's not only about instructions, also data fetches. But I'll compare them on the real data to see which is better
Pavel
For moves instead of swaps, it will definitely be better for big cases, but it will make it all more complex (not sure if I want to dive into that)
Aakash
It's not only about instructions, also data fetches. But I'll compare them on the real data to see which is better
Ok! Try with vector/tuple you will find 500+ instr reduction compared to existing code;
labyrinth
for acquire semantics, is it gonna invalidate all the cache in the cpu cache or only the interested parts?
Pavel
Is it a premature optimization you're talking about? ;)
Probably :) it's easy to check whether it gives any noticeable difference (splitting the loop or not splitting), and if not I will make it one loop.
Anonymous
And what C++20 changed?
oh, i was wrong. C++20 just forbid specialisations of std::swap in namespace std
Anonymous
Why not std::swap ?
a and b might have their own (faster) swap implementations not in std::
Aakash
for acquire semantics, is it gonna invalidate all the cache in the cpu cache or only the interested parts?
You mean incorporating barriers between keys & values? asm volatile.. @gameraccoon
Anonymous
a and b might have their own (faster) swap implementations not in std::
Argument dependent lookup is needed to find them
Anonymous
labyrinth
You mean incorporating barriers between keys & values? asm volatile.. @gameraccoon
in a qt blog about acquire and release semantics, it mentions that the cpu cache is invalidated so the data can be read from the main memory. I wonder if all the data in cpu cache is invalidated or it is just the part of the program's interest. I don't ring a bell for "key & value" that you mentioned.
Pavel
You mean incorporating barriers between keys & values? asm volatile.. @gameraccoon
No, just I'm thinking that sorting one array at a time can reduce cache misses, so will result in fewer fetches from main memory
Vitalii
sorry, void MyFormForLab::on_pushButton_clicked() { QString text = ui->comboBox->currentText(); ui->label->setStyleSheet("background-color:" + text); } It changes my background of the label, how to change the color of the text, not background? if to write just "color:".... it doesn't work.
Vitalii
https://www.w3schools.com/csS/css_text.asp
sorry, Didn't see the answer there(
Vitalii
void MyFormForLab::on_pushButton_clicked() { ui->label->setStyleSheet("{background-color : red; color: blue;}"); } If i write like this, then I have a problem: Could not parse stylesheet of object QLabel(0xfdee00, name = "label")
Igor🇺🇦
It should be the same configuration as css. Something like "QLabel {color: white; background-color: black}" Should make white text on black background
Igor🇺🇦
https://stackoverflow.com/questions/2749798/qlabel-set-color-of-text-and-background
𝓐𝓵𝓲
Guys I have a problem to two program in a one. I want to write a program to count digits and characters with a single input . I know how to write each of them but I don't know how to write them in one program . for example: input : Today is 22th of February output : Charakter : 19 digits 2
𝓐𝓵𝓲
I can send the codes here as well