User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 426,011 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 1,568 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser: Programming Forums
Views: 1378 | Replies: 42 | Solved
Reply
Join Date: Dec 2007
Posts: 363
Reputation: OmniX is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 1
OmniX's Avatar
OmniX OmniX is offline Offline
Posting Whiz

Arrays

  #1  
Jul 18th, 2008
I would like to make a "changable" array.

Meaning:
- Not initializing the size.
- Add arrays to the array.
- Remove arrays from the array.

I expect the array size to be 9million or something similar.

Was not sure the best plan of attack after googling and reading books.

Small example if you could please of the 3 points.

Thanks, Regards X.
"You never stop learning" - OmniX
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Oct 2006
Location: the Netherlands
Posts: 1,779
Reputation: niek_e is a name known to all niek_e is a name known to all niek_e is a name known to all niek_e is a name known to all niek_e is a name known to all niek_e is a name known to all 
Rep Power: 11
Solved Threads: 185
niek_e's Avatar
niek_e niek_e is offline Offline
Posting Virtuoso

Re: Arrays

  #2  
Jul 18th, 2008
The answer would be vectors:
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7. const int maxsize = 9000000;
  8. vector<int> lst;
  9. lst.resize(maxsize);
  10. for(int i = 0; i < maxsize; i++)
  11. lst[i] = 0;
  12. return 0;
  13. }

To add array's, you could use a vector of vectors, or a map
2d vectors:
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7. const int maxsize = 9000000;
  8. vector<int> lst;
  9. lst.resize(maxsize);
  10. for(int i = 0; i < maxsize; i++)
  11. lst[i] = 0;
  12. // 9 million elements in 1 vector
  13.  
  14. vector< vector <int> > array_vector;
  15. array_vector.push_back(lst);
  16. array_vector.push_back(lst);
  17. // now 2 vectors in array_vector, so 18 millions ints
  18. return 0;
  19. }
Want better/more replies to your questions? Wrap your code in [code] [/code] tags!
do NOT pm me for help, in the best case, you'll get ignored
Reply With Quote  
Join Date: Dec 2007
Posts: 363
Reputation: OmniX is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 1
OmniX's Avatar
OmniX OmniX is offline Offline
Posting Whiz

Re: Arrays

  #3  
Jul 18th, 2008
Very nice!

I heard vectors mentioned before (for what it matters I have no idea what vectors are )
Anyways two questions:
You have initialized the array, for my purposes I cant initialize it. Is this possible?

You have said to add an array but not to remove?

So is it possible to add/remove arrays from a not initialized array?

Sorry about the questions, just new

Thanks, Regards X

PS: I also require a function to break up the array into individual variables, how would I do that? Thanks Again
Last edited by OmniX : Jul 18th, 2008 at 4:37 am.
"You never stop learning" - OmniX
Reply With Quote  
Join Date: Oct 2006
Location: the Netherlands
Posts: 1,779
Reputation: niek_e is a name known to all niek_e is a name known to all niek_e is a name known to all niek_e is a name known to all niek_e is a name known to all niek_e is a name known to all 
Rep Power: 11
Solved Threads: 185
niek_e's Avatar
niek_e niek_e is offline Offline
Posting Virtuoso

Re: Arrays

  #4  
Jul 18th, 2008
Originally Posted by OmniX View Post
You have initialized the array, for my purposes I cant initialize it. Is this possible?

You have said to add an array but not to remove?

So is it possible to add/remove arrays from a not initialized array?

PS: I also require a function to break up the array into individual variables, how would I do that? Thanks Again


To answer all your questrions, I've made a small demo with vectors:
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4.  
  5. void show(vector <int> l)
  6. {
  7. cout << "\n\nIn vector: \n---------------------\n";
  8. for (unsigned int i = 0; i < l.size(); i++)
  9. cout << l[i] << "\n"; //access member in similar way to array's
  10. cout << "\n---------------------\n";
  11. cout << "size of vector = " << l.size() << "\nPress enter...";
  12. cin.get();
  13. }
  14.  
  15. int main()
  16. {
  17. vector<int> lst;
  18. lst.push_back(1); //add number
  19. show(lst);
  20. lst.push_back(2222);//add another number
  21. show(lst);
  22. lst.pop_back(); //pop the last number
  23. show(lst);
  24. return 0;
  25. }
Last edited by niek_e : Jul 18th, 2008 at 4:51 am.
Want better/more replies to your questions? Wrap your code in [code] [/code] tags!
do NOT pm me for help, in the best case, you'll get ignored
Reply With Quote  
Join Date: Dec 2007
Posts: 363
Reputation: OmniX is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 1
OmniX's Avatar
OmniX OmniX is offline Offline
Posting Whiz

Re: Arrays

  #5  
Jul 18th, 2008
Thanks niek_e, very sexy indeed!!!

I think the only thing left is
"I also require a function to break up the array into individual variables, how would I do that?"

I require:
- 5 variables which are used to make a string
- string is then brokern up into 5 variables
Eg.
input = 1, 2, 3, 4, 5
output = 1 2 3 4 5
further output = 1, 2, 3, 4, 5

Then I can try solve my problem.

Hope that makes sense, Any ideas?

Thankyou Regards, X
"You never stop learning" - OmniX
Reply With Quote  
Join Date: Oct 2006
Location: the Netherlands
Posts: 1,779
Reputation: niek_e is a name known to all niek_e is a name known to all niek_e is a name known to all niek_e is a name known to all niek_e is a name known to all niek_e is a name known to all 
Rep Power: 11
Solved Threads: 185
niek_e's Avatar
niek_e niek_e is offline Offline
Posting Virtuoso

Re: Arrays

  #6  
Jul 18th, 2008
To get individual elements of a vector:
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4.  
  5. int GiveMeNumber(vector <int> iV, int i)
  6. {
  7. if (i < iV.size() && i >= 0)
  8. return iV[i];
  9. else
  10. return -1;
  11. }
  12.  
  13. int main()
  14. {
  15. vector<int> lst;
  16. int elem, number;
  17. lst.push_back(1); //add number
  18. lst.push_back(2222);//add another number
  19. lst.push_back(333);//add another number
  20. lst.push_back(4567);//add another number
  21.  
  22. cout << "which element do you want?\n";
  23. cin >> elem;
  24. number = GiveMeNumber(lst, elem);
  25. if (number < 0)
  26. cout << "element doesn't exist, vector is " << lst.size() << " elements (0-" << lst.size()-1 <<")\n" ;
  27. else
  28. cout << "element " << elem << " contains number: " << number;
  29.  
  30. cin.ignore();
  31. cin.get();
  32. return 0;
  33. }

To break the string into separate numbers, you could use a stringstream and then use getline (with a delimiter to extract all the number.
In the following example, I assume that all the number are separated with a comma ( ',' ) and nothing more. I'm not going to do all your work for you

example:
  1. #include <iostream>
  2. #include <string>
  3. #include <sstream>
  4. using namespace std;
  5.  
  6.  
  7. int main()
  8. {
  9. stringstream ss;
  10. string input = "1,2,3,4,5";
  11. string buffer;
  12. ss << input;
  13. while (getline(ss,buffer,','))
  14. cout << buffer << "\n";
  15. cin.get();
  16. return 0;
  17. }


output:
1
2
3
4
5
Want better/more replies to your questions? Wrap your code in [code] [/code] tags!
do NOT pm me for help, in the best case, you'll get ignored
Reply With Quote  
Join Date: Dec 2007
Posts: 363
Reputation: OmniX is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 1
OmniX's Avatar
OmniX OmniX is offline Offline
Posting Whiz

Re: Arrays

  #7  
Jul 18th, 2008
Thanks for all the help niek_e!!!

I totally understand "it is better to teach a person how to fish then to give them a fish"

I will go back and apply the ideas you have mentioned.

Before I go 2 questions:
- You used stringstream to break up the string using a "," if i was using a space it would have just been " ", correct?

- You used buffer to get the broken value from the broken string. Now if I wanted to assign that value to an int I would have used one = buffer then two = buffer, etc, correct?

Thanks again, Regards X
"You never stop learning" - OmniX
Reply With Quote  
Join Date: Oct 2006
Location: the Netherlands
Posts: 1,779
Reputation: niek_e is a name known to all niek_e is a name known to all niek_e is a name known to all niek_e is a name known to all niek_e is a name known to all niek_e is a name known to all 
Rep Power: 11
Solved Threads: 185
niek_e's Avatar
niek_e niek_e is offline Offline
Posting Virtuoso

Re: Arrays

  #8  
Jul 18th, 2008
Originally Posted by OmniX View Post
I totally understand "it is better to teach a person how to fish then to give them a fish"

Very good attitude!

Originally Posted by OmniX View Post
Before I go 2 questions:
- You used stringstream to break up the string using a "," if i was using a space it would have just been " ", correct?

Almost. It would be ' ' (single quotes, because it's a char)

Originally Posted by OmniX View Post
- You used buffer to get the broken value from the broken string. Now if I wanted to assign that value to an int I would have used one = buffer then two = buffer, etc, correct?

Almost. () You would have to do some conversion from std::string (or char) to int. But there's plenty of sample code on the net on that subject. Good luck!
Want better/more replies to your questions? Wrap your code in [code] [/code] tags!
do NOT pm me for help, in the best case, you'll get ignored
Reply With Quote  
Join Date: Dec 2007
Posts: 363
Reputation: OmniX is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 1
OmniX's Avatar
OmniX OmniX is offline Offline
Posting Whiz

Re: Arrays

  #9  
Jul 18th, 2008
Ok ill get back to the drawing board and well see what I can come up with, Thanks for all the help on this thread and the other.

Ill keep you posted.

Thankyou Again, Enjoy your weekend
"You never stop learning" - OmniX
Reply With Quote  
Join Date: Dec 2007
Posts: 363
Reputation: OmniX is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 1
OmniX's Avatar
OmniX OmniX is offline Offline
Posting Whiz

Re: Arrays

  #10  
Jul 18th, 2008
Ok few errors, ill tackle one at a time:
- Unable to open file <sstream>
- Code may require debugging

  1. #include <iomanip>
  2. #include <iostream.h>
  3. #include <fstream.h>
  4. #include <sstream>
  5. #include <string>
  6. #include <vector>
  7.  
  8. string temp;
  9. string stringi;
  10. stringstream ss;
  11. vector<string> arrayi (6);
  12.  
  13. int one = 1, two = 2, three = 3;
  14. int four = 4, five = 5, six = 6;
  15.  
  16. for (int j=0; j<6; j++) {
  17. if(j==0) {
  18. stringi.append(one);
  19. stringi.append(" ");
  20. }
  21. if(j==1) {
  22. stringi.append(two);
  23. stringi.append(" ");
  24. }
  25. if(j==2) {
  26. stringi.append(three);
  27. stringi.append(" ");
  28. }
  29. if(j==3) {
  30. stringi.append(four);
  31. stringi.append(" ");
  32. }
  33. if(j==4) {
  34. stringi.append(five);
  35. stringi.append(" ");
  36. }
  37. if(j==5) {
  38. stringi.append(six);
  39. }
  40. }
  41.  
  42. ss << stringi;
  43. for (int i = 0; i < arrayi.size(); i++) {
  44. getline(ss, temp,' ');
  45. arrayi[i] = temp;
  46. }
  47.  
  48. if (one = array[i]) {
  49. cout << "ONE" << endl;
  50. }
  51. // etc
  52.  

Yes im a total newbie at C++ and would appericate any help, sorry

Thanks, Regards X
"You never stop learning" - OmniX
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C++ Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 12:59 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC