I am searching solution for half hour and no progress.
Consider the list
QStringList foo = {};
for (int i=1; i<8; i++){
foo << "0";
}
If some check box was clicked, I'd like to change the value of the list to "1".
So, for example, how to change the 3rd 0 by 1? Something like (pseudo code) foo.replace(3,"0","1").
Just apply the KISS principle ;)
foo[3] = "1";
I came across this thread with a similar issue, except the list was passed by reference so this option didn't work.
This line did work:
list->operator [](idx) = "val";
The operator function returns a modifiable instance at idx so you can call other functions of the type returned as well:
list->operator [](idx).prepend("val");
Credit to: https://blog.fearcat.in/a?ID=01500-adf1c894-647c-4d9c-9355-338be961e5df
Related
is there a way to implement bsearch() to find multiple instances of key.
for example: (obj*)bsearch(key=r,arr,elements,sizeof(obj),(int(*)(const void*, const void*)bcompare);
The code I currently wrote only finds the first instance and cannot proceed past the first found due to how it works.
getline(target,81);
if(strcmp(target,"exit") == 0 || strcmp(target, "") == 0) break;
p = (Info*)bsearch(target,list,num,sizeof(Info),(int(*)(const void*, const void*))bcompare);
int foundIndex = (int)(p-list);
if(!p){
err_dis1_win();
clrscr();
}
else{
display_record(p);
cout << "\n\n found at index " << foundIndex << "\n";
getch();
clrscr();
}
Variables:
p - is a pointer to object of class Info
target - arr of char
list - arr of obj
foundIndex - index of element found
Info - derived class from base class
**compare function
int bcompare(char *a,Info *b){
return(strncmpi(a, b -> get_name(), strlen(a)));
}
I cannot use other methods such as std::find or writing my own binary search function and have to use bsearch()
I have tried loops inside the else block, and the compare function using the varible foundIndex, as well as using a while loop on the return value looping through the obj list arr. Is there a way to start at a specific index. I appreciate any help. I am not looking for code but a general push in the right direction. Thank you.
Caveat - The current code compiles and runs as expected however, the functionality that I want, cannot be figured out by myself. Google and search on Stackoverflow has not produced an related issue.
Since bsearch() returns only one item, I interpret "find multiple instances of key" as "find the first instance of a key". The caller can then step forward through the array from that item to process each item matching the key, until it reaches the end or reaches an item that does not match.
If you must use the standard library's bsearch() function and persuade it to find the first item matching a given key, then all you really have to work with is the comparison function you present. bsearch() will return an item that matches the key according to that function, but if more than one item matches then there is no guarantee which one will be returned. You must ensure, then, that only the item you want matches.
You can approach that with an appropriate implementation of the comparison function, but there is a significant problem. The function will in some cases need to evaluate the item preceding the one specified to it, but it must not attempt to examine an item preceding the array's first. bsearch() does not itself convey any information about the array bounds to the comparison function.
There are at least two possible solutions, neither of them stellar.
Store the array lower bound in some well-known location that the function can access. For example, if the comparison function is a static member function, then maybe you would use a static variable of its class. But that is not thread-safe. You could do something similar with thread-local variables, but even then it's ugly. Either way, you have to be sure to set that variable appropriately before you call bsearch(), and that's ugly, too.
OR
Ensure that you never bsearch() for the first item. One way you could do that would be by checking preliminarily whether the first item matches (but not via the comparison function), and using it directly instead of calling bsearch() in the event that it does match. I'd wrap that in a method, myself, and if you must not do so then requiring that such a calling discipline be employed manually is also ugly.
Having chosen one of the above, you can implement a comparison function that looks at the previous item's key in addition to the specified item's. Something along these lines (which assumes the second alternative):
struct my_item {
int key;
void *data;
};
// bsearch() passes the target item as the first argument, and the one to compare
// to it as the second
int compare_items(const void *to_find, const void *to_check) {
const struct my_item *to_find_item = (const struct my_item *) to_find;
const struct my_item *to_check_item = (const struct my_item *) to_check;
// Check first how the key members are ordered
if (to_find_item->key < to_check_item->key) {
return -1;
} else if (to_find_item->key > to_check_item->key) {
return 1;
} else {
// The key members match, so check whether we're looking at the first
// such item.
const struct my_item *previous_item = to_check_item - 1;
// If the previous item's key does match, then we know the item we're
// looking for is an earlier one than we are presently checking.
return (previous_item->key == to_check_item->key) ? -1 : 0;
}
}
I seen weird for loop syntax in C++. Please see following program.
#include <iostream>
using namespace std;
int main()
{
int num, count = 0;
int array[]= {1,1,2,3,4,1,3,2,9,8,7};
cout<<"Which number would you like to know about how many times it appeared?"<<endl;
cin>>num;
for (int i:array)
{
if (i == num)
++count;
}
cout<<"Number "<<num<<" appeared "<<count<<" times in the array"<<endl;
return 0;
}
It is successfully run on GCC Linux platform.
Reference link Here.
So, My question is, Is it the correct way to use for loop in C++?
Also, Is for (int i:array) equivalent to for ( int i:array ; ; )?
Sorry for my poor english. Thanks in advance.
There are now (since C++11) two distinct syntaxes for for-loops, the old C-style:
for (initialization; test; increment)
and the new
for (declaration: container)
In the new syntax, the declaration declares a variable which is successively given the value of each element of the container. Common values of "declaration" are auto val, const auto val, auto& val, and const auto& val, depending on whether you want a reference to the value in the container or a copy, and whether you want the value constant or not.
Both syntaxes are correct. It rather depends what you want to do in the loop. My preference is to use the range based for unless I am going to need the loop counter or iterator ... in which case I fall back on the old style for.
See http://en.cppreference.com/w/cpp/language/range-for for the gory details of the specification (and what is meant by "container").
The syntax for (int i:array) iterates through each element in the array, compared to for (int i = 0; i<sizeof(array); i++) which creates a counter that automatically increments on each iteration of the loop. The counter can then be used to access elements of the array with array[i]
As for which one you'd use, it depends on what you want to do. In your example there isn't a need to keep track of which iteration of the loop you are on, so the former will work fine. If you wanted to, say, print the iteration number each time then you would use the latter.
P.S. your English is perfect :)
As a course project, I am building a lottery machine simulator with different kinds of prizes.
I have a class called LotteryTicket which holds a vector<int> luckyNumbers as one of its private member variables. In order to access it, I have defined a get function as following:
vector<int> LotteryTicket::getLuckyNumbersList()
{
return luckyNumbers;
}
One of the things I am required to do is check for winning tickets. A "Bronze" winner is someone who managed to guess all the correct numbers, but not in the correct order. In other words, the sets have to match. In order to do that, I want to sort both tickets (the winning numbers and the player's numbers) and then check 1 for 1.
I attempt to sort them by using the following functions:
void LotteryMachine::sortTicket(LotteryTicket& ticket)
{
for(int i = ticket.getLuckyNumbersList().size()-1; i >= 0; i--) {
for(int j = 0; j < i; j++) {
if(ticket.getLuckyNumbersList()[j] > ticket.getLuckyNumbersList()[j+1])
swap(ticket.getLuckyNumbersList()[j], ticket.getLuckyNumbersList()[j+1]);
}
}
cout << endl << "Sorted ticket " << ticket.getTicketNumber() << ":";
printTicket(ticket);
}
//sorting by bitwise operations
void LotteryMachine::swap(int& a, int& b)
{
a ^= b;
b ^= a;
a ^= b;
}
I have the cout there as a test to see if my vectors were indeed sorted. So far no luck. I am suspecting I'm losing reference at some point, but I'm not sure where... or I might messed something up in my syntax with all these long dot-notation returns...
For additional context, the sortTicket(LotteryTicket&) function is invoked from within the function that checks if a ticket is a bronze winner. That mentioned function receives the ticket argument from a different function... i.e. a long chain of passing the argument, all with a call by reference (I can paste more code if needed.)
Any thoughts? The sortTicket function should sort the arrays and make it stick outside the scope of the function.
vector<int> LotteryTicket::getLuckyNumbersList()
You made a copy when getLuckyNumbersList() returns since you weren't returning a reference.
Change into the following instead.
vector<int>& LotteryTicket::getLuckyNumbersList()
1) I want to pass a the pointer of a QVector to a function and then do things with it. I tried this:
void MainWindow::createLinearVector(QVector<float> *vector, float min, float max )
{
float elementDiff=(max-min)/(vector->size()-1);
if(max>min) min -= elementDiff;
else min += elementDiff;
for(int i=0; i< vector->size()+1 ; i++ )
{
min += elementDiff;
*(vector+i) = min; //Problematic line
}
}
However the compiler gives me "no match for operator =" for the *(vector+i) = min; line. What could be the best way to perform actions like this on a QVector?
2) The function is supposed to linearly distribute values on the vector for a plot, in a way the matlab : operator works, for instance vector(a:b:c). What is the simpliest and best way to perform such things in Qt?
EDIT:
With help from here the initial problem is solved. :)
I also improved the metod in itself. The precision could be improved a lot by using linear interpolation instead of multiple additions like above. With multiple addition an error is accumulating, which is eliminated in large part by linear interpolation.
Btw, the if statement in the first function was unecessary and possible to remove by just rearranging stuff a little bit even in the multiple addition method.
void MainWindow::createLinearVector(QVector<double> &vector, double min, double max )
{
double range = max-min;
double n = vector.size();
vector[0]=min;
for(int i=1; i< n ; i++ )
{
vector[i] = min+ i/(n-1)*range;
}
}
I considered using some enchanced loop for this, but would it be more practical?
With for instance a foreach loop I would still have to increment some variable for the interpolation right? And also make a conditional for skipping the first element?
I want to place a float a certain place in the QVector.
Then use this:
(*vector)[i] = min; //Problematic line
A vector is a pointer to a QVector, *vector will be a QVector, which can be indiced with [i] like any QVector. However, due to precedence, one needs parentheses to get the order of operations right.
I think, first u need use the Mutable iterator for this stuff: Qt doc link
Something like this:
QMutableVectorIterator<float> i(vector);
i.toBack();
while (i.hasPrevious())
qDebug() << i.{your code}
Right, so it does not make much sense to use a QVector pointer in here. These are the reasons for that:
Using a reference for the method parameter should be more C++'ish if the implicit sharing is not fast enough for you.
Although, most of the cases you would not even need a reference when just passing arguments around without getting the result back in the same argument (i.e. output argument). That is because *QVector is implicitly shared and the copy only happens for the write as per documentation. Luckily, the syntax will be the same for the calling and internal implementation of the method in both cases, so it is easy to change from one to another.
Using smart pointers is preferable instead of raw pointers, but here both are unnecessarily complex solutions in my opinion.
So, I would suggest to refactor your code into this:
void MainWindow::createLinearVector(QVector<float> &vector, float min, float max)
{
float elementDiff = (max-min) / (vector.size()-1);
min += ((max>min) ? (-elementDiff) : elementDiff)
foreach (float f, vector) {
min += elementDiff;
f = min;
}
}
Note that I fixed up the following things in your code:
Reference type parameter as opposed to pointer
"->" member resolution to "." respectively
Ternary operation instead of the unnatural if/else in this case
Qt's foreach instead of low-level indexing in which case your original point becomes moot
This is then how you would invoke the method from the caller:
createLinearVector(vector, fmin, fmax);
I just wonder if I can use a "complicated" map as the value of another map. I have self-defined several structs as follow:
typedef std::vector<std::string> pattern;
typedef std::map<int, std::vector<pattern>> dimPatternsMap;
typedef std::map<int, dimPatternsMap> supportDimMapMap;
OK let me explain these things...pattern is a vector of strings. For the "smaller" map dimPatternsMap, the key is an integer which is the dimension of pattern (the size of that vector containing strings) and the value is vector containing patterns (which is a vector of vectors...).
The "bigger" map supportDimMapMap also use an integer as the key value, but use dimPatternsMap as its value. The key means "support count".
Now I begin to construct this "complicated" map:
supportDimMapMap currReverseMap;
pattern p = getItFromSomePlace(); //I just omit the process I got pattern and its support
int support = getItFromSomePlaceToo();
if(currReverseMap.find(support) == currReverseMap.end()) {
dimPatternsMap newDpm;
std::vector<pattern> newPatterns;
newPatterns.push_back(currPattern);
newDpm[dim] = newPatterns;
currReverseMap[support] = newDpm;
} else{
dimPatternsMap currDpm = currReverseMap[support];
if(currDpm.find(dim) == currDpm.end()) {
std::vector<pattern> currDimPatterns;
currDimPatterns.push_back(currPattern);
currDpm[dim] = currDimPatterns;
} else {
currDpm[dim].push_back(currPattern);
}
}
Forgive me the code is really a mass...
But then as I want to traverse the map like:
for(supportDimMapMap::iterator iter = currReverseMap.begin(); iter != currReverseMap.end(); ++iter) {
int support = iter->first;
dimPatternsMap dpm = iter->second;
for(dimPatternsMap::iterator ittt = dpm.begin(); ittt != dpm.end(); ++ittt) {
int dim = ittt->first;
std::vector<pattern> patterns = ittt->second;
int s = patterns.size();
}
}
I found the value s is always 1, which means that for each unique support value and for each dimension of that support value, there is only one pattern! But as I debug my code in the map constructing process, I indeed found that the size is not 1 - I actually added the new patterns into the map successfully...But when it comes to traversing, all the sizes become 1 and I don't know why...
Any suggestions or explanations will be greatly appreciated! Thanks!!
dimPatternsMap currDpm = currReverseMap[support];
currDpm is a copy of currReverseMap[support]. It is not the same object. So then when you make changes to currDpm, nothing within currReverseMap changes.
On the other hand, if you use a reference:
dimPatternsMap& currDpm = currReverseMap[support];
then currDpm and currReverseMap[support] really are the same object, so later statements using currDpm will really be changing a value within currReverseMap.
There are a few other places where your code could benefit from references too.
My guess: you should use a reference in your else:
dimPatternsMap& currDpm = currReverseMap[support];
Your current code creates a copy instead of using the original map.
Your problem is this line:
dimPatternsMap currDpm = currReverseMap[support];
Based on the code following it, it wants to read like this:
dimPatternsMap& currDpm = currReverseMap[support];
Without the & you modify a copy of the entry rather than the existing entry.
Your code is making several copies of the objects underneath, try using more references and iterators (find() already gives you an element if it was found, for example).
For example, dimPatternsMap currDpm = currReverseMap[support]; actually makes a copy of a map in your structure and adds an element to it (not to the original). Try using a reference instead.