-
April 25th, 2015, 02:40 AM
#1
using std::list with a structure
if i have a struct like this
Code:
struct combi_t
{
int val0;
int val1;
int val2;
combi_t* nextPtr;
and i want to implement std::list loading types of combi_t,
so i do
Code:
std::list<combi_t> head;
how do i load each member of combi t into head ? at every head.push_back() how do i add members val0, val1, val2? thanks in advance
-
April 25th, 2015, 03:38 AM
#2
Re: using std::list with a structure
One way is to implement a constructor for combi_t. Example
Code:
#include <list>
using namespace std;
struct combi_t
{
int val0;
int val1;
int val2;
combi_t* nextPtr;
combi_t(int v0 = 0, int v1 = 0, int v2 = 0, combi_t* np = nullptr) : val0(v0), val1(v1), val2(v2), nextPtr(np) {}
};
int main()
{
list<combi_t> head;
head.push_back(combi_t(1, 2, 3));
head.push_back(combi_t(2, 3, 4));
}
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
April 25th, 2015, 04:21 AM
#3
Re: using std::list with a structure
I knew you Master 2kaud will save the day! you who gives the correct answer all the time and you save my a** from hotwaters!
THANK YOU AGAIN!
-
April 25th, 2015, 04:49 AM
#4
Re: using std::list with a structure
excuse me Master 2kaud,
what about accessing individual values of a type combi_t?
let say i have a function
void do_something(int) ;
and i want to call do_something 3 times but with each member of a type combi_t...
how do i do that? thanks in advance
if i had implemented my own list i could have done something like ptr->val0, ptr->val1, etc
-
April 25th, 2015, 04:56 AM
#5
Re: using std::list with a structure
One simple way is as below. If you are after something different you'll need to provide more details.
Code:
combi_t ct(3, 4, 5);
do_something(ct.val0);
do_something(ct.val1);
do_something(ct.val2);
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
April 25th, 2015, 04:59 AM
#6
Re: using std::list with a structure
-
April 25th, 2015, 07:43 AM
#7
Re: using std::list with a structure
excuse me again master a lil problem just popped up
implemented the struct like you taught..
i have a
Code:
std::list<combi_t > head;
std::vector<int> ups={2,3,4,5,6,7,8 ,9,};
void make_three_sures (std::vector <int> & cross)
{
for(auto p = cross.begin() ; p != cross.end(); p++)
{
for(auto pt = cross.begin()++; pt != cross.end() ; pt++)
{
head.push_back(combi_t( *p, *pt, *(pt++) ) );
}
}
}
int main()
{
make_three_sures(ups);
for (auto ptr = head.begin(); ptr != head.end() ; ptr = ptr++)
{
master_roller(combi_t.v0, combi_t.v1, combi_t.v2, num); //this call is wrong!!!
}
}
how can i go about it in this particula situation? sorry for the disturbances and thanks in advance
Last edited by TheLionKing; April 25th, 2015 at 07:45 AM.
-
April 25th, 2015, 08:11 AM
#8
Re: using std::list with a structure
Code:
master_roller(combi_t.v0, combi_t.v1, combi_t.v2, num); //this call is wrong!!!
combi_t is a class type, not a variable of a class instance. In my simple example in post #5, ct is the class instance. I suspect you need
Code:
master_roller(ptr->val0, ptr->val1, ptr->val2, num);
as ptr is a pointer to an instance of class type combi_t (rather than being an actual instance).
You could also have (not tried)
Code:
for (const auto& val : head)
master_roller(val.val0, val.val1, val.val2);
Last edited by 2kaud; April 25th, 2015 at 08:26 AM.
Reason: changed v0 etc to val0 etc
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
April 25th, 2015, 08:14 AM
#9
Re: using std::list with a structure
ok thanks..
i was doing try and error and did this
for (auto ptr = head.begin(); ptr != head.end() ; ptr = ptr++)
{ combi_t ct = *ptr;
master_roller(ct.val1, ct.val1, ct.val2, num);
}
seems to work, now let me look into yours too..thanks soooo much!!!
-
April 25th, 2015, 08:24 AM
#10
Re: using std::list with a structure
None of those solutions worked master, compiler telling me v0 not a member type, or val0 not a member type,
what worked was
for (auto ptr = head.begin(); ptr != head.end() ; ptr = ptr++)
{ combi_t ct = *ptr;
master_roller(ct.val1, ct.val1, ct.val2, num);
}
thank you a lot master 2kaud, the best ever on this site!!!
-
April 25th, 2015, 08:29 AM
#11
Re: using std::list with a structure
Originally Posted by TheLionKing
None of those solutions worked master, compiler telling me v0 not a member type, or val0 not a member type,
what worked was
thank you a lot master 2kaud, the best ever on this site!!!
Yes, I realised after I posted as I was just using v0, v1 etc from your post #7. As you say, it should be val0, val1 etc as per my changed code in post #8.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
April 25th, 2015, 08:31 AM
#12
Re: using std::list with a structure
ok sir! you're the best, in terms of knowledge and most important,PATIENCE!!!
thanks so much and keep up the good work!
-
April 25th, 2015, 08:42 AM
#13
Re: using std::list with a structure
For completeness. Example of two ways of iterating a container (c++11)
Code:
#include <list>
#include <iostream>
#include <iomanip>
using namespace std;
struct combi_t
{
int val0;
int val1;
int val2;
combi_t* nextPtr;
combi_t(int v0, int v1, int v2, combi_t* np = nullptr) : val0(v0), val1(v1), val2(v2), nextPtr(np) {}
};
void master_roller(int v0, int v1, int v2)
{
cout << setw(7) << v0 << setw(7) << v1 << setw(7) << v2 << endl;
}
int main()
{
list<combi_t> head;
head.push_back(combi_t(1, 2, 3));
head.push_back(combi_t(2, 3, 4));
for (const auto& v : head)
master_roller(v.val0, v.val1, v.val2);
for (auto pt = head.cbegin(); pt != head.cend(); ++pt)
master_roller(pt->val0, pt->val1, pt->val2);
}
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
April 25th, 2015, 08:50 AM
#14
Re: using std::list with a structure
thank you sir!
since we're already at it, another last question, so i implemented this two versions of codes to make a three combinations of numbers...
i really think they don't work well and now is it looping like mad..can you modify it a lil bit to make it better?
Code:
void make_three_sures (std::vector<int> & cross)
{
/*for(auto p = cross.begin() ; p != cross.end(); p++)
{
for(auto pt = cross.begin()++; pt != cross.end() ; pt++)
{
head.push_back(combi_t( *p, *pt, *(pt++) ) );
}
}*/
for (int p = 0; p < cross.size(); p++)
{ for(int pt = (p+1); pt < cross.size(); pt++)
{ if (pt>cross.size()) break;;
head.push_back(combi_t( cross[p], cross[pt], cross[pt++]) );
}
}
}
-
April 25th, 2015, 08:55 AM
#15
Re: using std::list with a structure
What are you trying to achieve?
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|