•
•
•
•
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
![]() |
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.
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
•
•
Join Date: Oct 2006
Location: the Netherlands
Posts: 1,779
Reputation:
Rep Power: 11
Solved Threads: 185
The answer would be vectors:
To add array's, you could use a vector of vectors, or a map
2d vectors:
cpp Syntax (Toggle Plain Text)
#include <iostream> #include <vector> using namespace std; int main() { const int maxsize = 9000000; vector<int> lst; lst.resize(maxsize); for(int i = 0; i < maxsize; i++) lst[i] = 0; return 0; }
To add array's, you could use a vector of vectors, or a map
2d vectors:
cpp Syntax (Toggle Plain Text)
#include <iostream> #include <vector> using namespace std; int main() { const int maxsize = 9000000; vector<int> lst; lst.resize(maxsize); for(int i = 0; i < maxsize; i++) lst[i] = 0; // 9 million elements in 1 vector vector< vector <int> > array_vector; array_vector.push_back(lst); array_vector.push_back(lst); // now 2 vectors in array_vector, so 18 millions ints return 0; }
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
do NOT pm me for help, in the best case, you'll get ignored
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
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
•
•
Join Date: Oct 2006
Location: the Netherlands
Posts: 1,779
Reputation:
Rep Power: 11
Solved Threads: 185
•
•
•
•
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:
cpp Syntax (Toggle Plain Text)
#include <iostream> #include <vector> using namespace std; void show(vector <int> l) { cout << "\n\nIn vector: \n---------------------\n"; for (unsigned int i = 0; i < l.size(); i++) cout << l[i] << "\n"; //access member in similar way to array's cout << "\n---------------------\n"; cout << "size of vector = " << l.size() << "\nPress enter..."; cin.get(); } int main() { vector<int> lst; lst.push_back(1); //add number show(lst); lst.push_back(2222);//add another number show(lst); lst.pop_back(); //pop the last number show(lst); return 0; }
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
do NOT pm me for help, in the best case, you'll get ignored
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
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
•
•
Join Date: Oct 2006
Location: the Netherlands
Posts: 1,779
Reputation:
Rep Power: 11
Solved Threads: 185
To get individual elements of a vector:
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:
output:
cpp Syntax (Toggle Plain Text)
#include <iostream> #include <vector> using namespace std; int GiveMeNumber(vector <int> iV, int i) { if (i < iV.size() && i >= 0) return iV[i]; else return -1; } int main() { vector<int> lst; int elem, number; lst.push_back(1); //add number lst.push_back(2222);//add another number lst.push_back(333);//add another number lst.push_back(4567);//add another number cout << "which element do you want?\n"; cin >> elem; number = GiveMeNumber(lst, elem); if (number < 0) cout << "element doesn't exist, vector is " << lst.size() << " elements (0-" << lst.size()-1 <<")\n" ; else cout << "element " << elem << " contains number: " << number; cin.ignore(); cin.get(); return 0; }
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:
cpp Syntax (Toggle Plain Text)
#include <iostream> #include <string> #include <sstream> using namespace std; int main() { stringstream ss; string input = "1,2,3,4,5"; string buffer; ss << input; while (getline(ss,buffer,',')) cout << buffer << "\n"; cin.get(); return 0; }
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
do NOT pm me for help, in the best case, you'll get ignored
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
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
•
•
Join Date: Oct 2006
Location: the Netherlands
Posts: 1,779
Reputation:
Rep Power: 11
Solved Threads: 185
•
•
•
•
I totally understand "it is better to teach a person how to fish then to give them a fish"
Very good attitude!
•
•
•
•
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)•
•
•
•
- 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
do NOT pm me for help, in the best case, you'll get ignored
Ok few errors, ill tackle one at a time:
- Unable to open file <sstream>
- Code may require debugging
Yes im a total newbie at C++ and would appericate any help, sorry
Thanks, Regards X
- Unable to open file <sstream>
- Code may require debugging
cpp Syntax (Toggle Plain Text)
#include <iomanip> #include <iostream.h> #include <fstream.h> #include <sstream> #include <string> #include <vector> string temp; string stringi; stringstream ss; vector<string> arrayi (6); int one = 1, two = 2, three = 3; int four = 4, five = 5, six = 6; for (int j=0; j<6; j++) { if(j==0) { stringi.append(one); stringi.append(" "); } if(j==1) { stringi.append(two); stringi.append(" "); } if(j==2) { stringi.append(three); stringi.append(" "); } if(j==3) { stringi.append(four); stringi.append(" "); } if(j==4) { stringi.append(five); stringi.append(" "); } if(j==5) { stringi.append(six); } } ss << stringi; for (int i = 0; i < arrayi.size(); i++) { getline(ss, temp,' '); arrayi[i] = temp; } if (one = array[i]) { cout << "ONE" << endl; } // etc
Yes im a total newbie at C++ and would appericate any help, sorry

Thanks, Regards X
"You never stop learning" - OmniX
![]() |
•
•
•
•
•
•
•
•
DaniWeb C++ Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Similar Threads
- (reformatted) How to return Multi-Dimensional Arrays (C++)
- What relation does **indirection operator have with Multidimensional Arrays (C++)
- Arrays (C++)
- How to Return Multidimensional Arrays (C++)
- C file input/output 2D arrays. (C)
- passing arrays in visual basic (Visual Basic 4 / 5 / 6)
Other Threads in the C++ Forum
- Previous Thread: making a simple c++ game
- Next Thread: Declare a 3D vector



Linear Mode