Even and odd items in a list C++
Dec 17, 2015 at 2:31pm UTC
Hi, I have this list with items. I want the items in the list 0,2,4,6 and so on depening on the number of items the user put in the list.
After selecting the even numbers do I want to randomize the even numbers in to a textbox in windows form. But the following item after the even one is connected to the first one.
1 2 3 4 5 6 7
i1 = glosa->Count;
srand (time(NULL));
if (i1%2 != 0)
i1 += 1;
i1= rand()% glosa->Count;
i = i1;
tbx_SvarSv->AppendText(glosa[i]);
This code is printing all of the items in the list in a randome order. This is wrong.
Thanks.
Dec 17, 2015 at 3:08pm UTC
Instead of trying to relate an item to the adjacent item, if they must stay together, perhaps you should use some data structure to keep them together.
A
struct
or
class
would do.
1 2 3 4
struct group {
string one;
string two;
}
then instead of a list of strings, use a list of group objects.
Or use the std::pair
http://www.cplusplus.com/reference/utility/pair/
Dec 17, 2015 at 3:10pm UTC
How do I use the std::pair funtion in a list full of strings?
Last edited on Dec 17, 2015 at 3:11pm UTC
Dec 17, 2015 at 5:04pm UTC
You would replace the 'list full of strings' with a list full of pairs.
Each pair would contain two strings.
Dec 17, 2015 at 6:07pm UTC
I'm so greatful for your help! How would you write a code with list full of pairs. Never used the pair funtion before! BIG thanks!
Dec 17, 2015 at 6:23pm UTC
Just an example code to give you some ideas. Here I used a vector rather than a list to store the data. Only reason for that was so I could use the random_shuffle.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
#include <iostream>
#include <string>
#include <utility>
#include <vector>
#include <sstream>
#include <algorithm>
std::istringstream fin (
"peach 1 "
"apple 2 "
"orange 3 "
"grape 4 "
"banana 5 "
);
int main()
{
typedef std::pair<std::string, std::string> item; // for simplicity
std::vector<item> items; // container
std::string one, two;
while (fin >> one >> two) // get the input and store it
items.push_back(make_pair(one, two));
std::cout << "\n\n----- input -----\n\n" ; // display what we have
for (auto &a : items)
std::cout << a.first << " " << a.second << '\n' ;
std::cout << "\n\n----- sort ------\n\n" ; // sort and redisplay
std::sort(items.begin(), items.end());
for (auto &a : items)
std::cout << a.first << " " << a.second << '\n' ;
std::cout << "\n\n--- shuffle -----\n\n" ; // shuffle and redisplay
std::random_shuffle(items.begin(), items.end());
for (auto &a : items)
std::cout << a.first << " " << a.second << '\n' ;
}
Sample output:
----- input -----
peach 1
apple 2
orange 3
grape 4
banana 5
----- sort ------
apple 2
banana 5
grape 4
orange 3
peach 1
--- shuffle -----
peach 1
banana 5
orange 3
grape 4
apple 2
Dec 17, 2015 at 6:28pm UTC
Thats a great code for me to use! I'm currently having problem to use this with windows form. How do I modify it to be used with windows form?
Dec 17, 2015 at 6:57pm UTC
How do I modify it to be used with windows form?
First, you can re-write the loops I used if necessary
1 2
for (auto &a : items)
std::cout << a.first << " " << a.second << '\n' ;
1 2
for (unsigned int i=0; i<items.size(); i++)
std::cout << items[i].first << " " << items[i].second << '\n' ;
Either way, instead of printing the first and second,, you would I guess want to use something like
1 2
AppendText(a.first)
AppendText(a.second)
or
1 2
AppendText(items[i].first)
AppendText(items[i].second)
or something like that.
You may need to ask someone else to help, if you can't figure it out yourself.
Dec 17, 2015 at 7:18pm UTC
I do not get the pair funtion to work in my project at all. There is a syntex error for:
pair <String^, String^> item;
Do you know why?
I will try to get someone, thanks.
Dec 17, 2015 at 7:32pm UTC
here I am out of my depth as this isn't standard C++ at all, it's some sort of Microsoft proprietary language.
Dec 17, 2015 at 7:36pm UTC
I see, its called "Windows From Project". I'm having a hard time to translate orginal C++ code to the windows form format. Thanks anyways!
Dec 17, 2015 at 7:40pm UTC
If you do a google search for "std::string to String^" there are lots of links, some may be what you want.
Topic archived. No new replies allowed.