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.
Related
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 do I correctly ask for object rock in main. I simplified the code, figuring this was the only problem. The error "expected primary-expression before '&' token.
void createObject(vector <object>& obj, world wld)
{
....
}
int main()
{
object rock;
createObject(vector<object>& rock, level_1);
return 0;
}
Very simply:
int main()
{
std::vector<object> rock_vector(1);
createObject(rock_vector, level_1);
}
You can't pass rock to it, as it's not a vector. You need to pass an actual vector to it. Here, I made rock_vector of size 1, so it's at least got one object in it (so rock_vector[0] is more or less your replacement for rock).
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));
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