Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
How to use parallel_sort with this struct:
struct d
{
char name[5];
};
struct less_than_key
{
inline bool operator() (const d& struct1, const d& struct2) const
{
return strcmp(struct1.name, struct2.name) == -1;
}
};
vector<d> my_d;
my_d.resize(3);
strcpy(my_d[0].name, "mike");
strcpy(my_d[1].name, "joe");
strcpy(my_d[2].name, "anna");
parallel_sort(my_d.begin(), my_d.end(), less_than_key());
DONE!, I hope this helps someone else.
Guess it's time to take my Phd in sorting algorithms!
Don't use C-style strings when you can easily use std::string.
Since your struct only contains a string you may want to just use std::string instead (which implements the operators that you need for parallel_sort to work).
std::vector<std::string> vec(3);
vec.emplace_back("mike");
vec.emplace_back("joe");
vec.emplace_back("anna");
and then, following the function signature, you just need to find the begin and end iterator:
parallel_sort(std::begin(vec), std::end(vec));
Related
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
Consider the following source code:
map<string,vector<SectionElement *>> _sections;
...
static SharedData *_shared;
...
static int iniHandler(void* user, const char* section, const char* name,
const char* value)
{
map<string,vector<SectionElement*>> iniFile = *(_shared->sections);
auto& iniSection = iniFile[ section];
auto sectionElement = new SectionElement();
sectionElement->name = name;
sectionElement->value = value;
iniSection.push_back( sectionElement);
return 1;
}
The problem with the code is that if I add an element to iniSection it works, but the vector that is retrieved from iniFile does not seem to be the same that is kept in the map. So every time the function iniHandler is called the count of the vector is zero. I am a bit at a loss here and wondering what obvious thing I am missing...
You modify a local map called iniFile. This has no effect on some other map, *(_shared->sections). Perhaps you wanted to make iniFile a reference?
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I'd like to write a function that will return 4 members of a class. They are 3 ints and char, and I'd like to store them all in one vector and return it from a function call. Can I do that?
You either need an std::tuple if you want to preserve the types and if the result length is constant, or just cast all members to some common supertype and store them in a container.
You need a class:
struct S
{
int a, b, c;
char letter;
};
int main()
{
S s;
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Using the following code:
class MyClass
{
public:
void GetStrings(list<std::string>& strs)
{
strs = m_strings;
}
list<std::string> m_strings;
}
Are all m_strings's elements being copied when strs = m_strings; is performed?
In other words:
Is this equal to list<std::string>& strs = m_strings; or to list<std::string> strs = m_strings; (with respect to elements copy)
yes.
Now, if you wanted to copy just a reference to the list (like you have in languages such as C# or Java) then pass a shared_ptr instead, then you will have a single list with 2 references to it.
All variable assignment in C/C++ is done by value, even if the variable being passed is a pointer (eg a 4 byte variable). Other languages are the same, its just that they cover this up with their language constructs.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a class Samp. In Samp.cpp, I can define/declare a function like
Samp& operator+(Samp& other) {
std::cout << "something";
return other;
}
What is this function exactly? How do I call it?
This is actually a unary +, you call it like this:
Samp s;
+s; // <-- here
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
For example:
string str[3];
void foo(char** str)
{
//do something to str...
}
How to pass str[] to function foo in a convenient way?
The function expects an array of pointers, so you'll have to make one from your array of strings:
std::vector<char*> pointers;
for (auto & s : str) {
pointers.push_back(&s[0]);
}
foo(&pointers[0]);
Beware that this may not be valid if the function modifies the pointers, or the strings they point to. A better option would be to avoid mixing C and C++ style string handling, if possible.