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.

The answer would be vectors:

#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](http://www.cppreference.com /cppmap/index.html)
2d vectors:

#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;
}

Edited 3 Years Ago by Nick Evan: Fixed formatting

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

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:

#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;
}

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

To get individual elements of a vector:

#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:

#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
Comments
Good posts in this thread.

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"

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!

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 :)

Ok few errors, ill tackle one at a time:
- Unable to open file <sstream>
- Code may require debugging

#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

hmmm.. That's not quite what I meant :)
How about something like:

#include <iostream>
#include <string>
#include <sstream>
#include <vector>
using namespace std;


int main()
{
    vector <int> all_numbers;
    stringstream ss;
    string input  = "1,2,3,4,5";
    string buffer;
    int integer;
    ss << input;
    while (getline(ss,buffer,','))
    {
        //convert buffer to int and put in 'integer'
        istringstream strin(buffer);
        strin >> integer;
        // integer now contains the numeric value of the buffer
        //so now push it in the vector
        all_numbers.push_back(integer);
    }
    cout << "Pushed " << all_numbers.size() << " numbers in the vector";
    cin.get();
    return 0;
}

output: Pushed 5 numbers in the vector When this code is finished, you will have a vector containing 5 integers. But you can add as much (..) numbers to the string and the vector will dynamically expand.

You can also use the GiveMeNumber() and Show() functions I posted in previous posts on this program.

This code might be a bit much for you, but if you have any question, just ask and I'll try to answer them.

Ok few errors, ill tackle one at a time:
- Unable to open file <sstream>

You're using the (ancient) borland compiler right? Perhaps changing it to #include <sstream.h> will work. But I'm not even sure if Borland already supported stringstreams.... I strongly suggest that you upgrade to a modern IDE+compiler as suggested in one of the other thread :)

Ok I have just downloaded and installed "Visual C++ 2008 Express".

Now it is giving me an error about the goto statement label. "xxxxx:" displaying "error C2143: syntax error : missing ';' before '}'".

Should I just try another compiler or ????

The code was working in Borland so I dont see why when I copy and paste it into VC++ that it should not work :(

Thanks, Regards X

Ok I have just downloaded and installed "Visual C++ 2008 Express".

Now it is giving me an error about the goto statement label. "xxxxx:" displaying "error C2143: syntax error : missing ';' before '}'".

Should I just try another compiler or ????

The code was working in Borland so I dont see why when I copy and paste it into VC++ that it should not work :(

Thanks, Regards X

Visual Studio 2008 Express should be able to handle niek's code. I can't imagine why it wouldn't. I copied and pasted niek's code into Dev C++ and it compiled and ran fine, and I did the same with Visual Studio 2008 (non-Express edition) and it did fine there too. I don't see any goto statements in niek's code. You started a console project, right? Try copying and pasting niek's code above into Visual Studio 2008 again and seeing if it compiles and runs. It should, I think. The error you list sounds like a typo somewhere, maybe?

Nothing wrong with niek_e code, its the one im working on.

I have changed compilers to codeblocks and its working no, so problem solved.

Onto the next one.

Im not sure If I have current brain freeze of trying to get my head around the new programming concept of C++ but from niek_e previous example I am trying to do exactly what he did BUT instead of just one variable I trying to break up the whole string into individual variables then compare them and then reattach all the variables as a string and add the "whole" variable to the string array.

Are you able to compare string vs. string in C++ or string vs. int?

Thanks, Regards X

PS: I apologize again about the newbieness :(

I was revising my code, it may have to do with my vectors being a "string"???

//int c_same = 0;

//strings
string c_array_temp;
string c_string;
string c_temp;
stringstream c_stringstream;

//vectors
vector<string> c_array;

// Checks if 3 numbers are the same
for (int numS= 0; numS< combinations_array.size(); numS++) {
 c_stringstream << c_array[numS];
 for (int numB= 0; numB< combinations_array.size(); numB++) {
 getline(c_stringstream, c_temp,' ');
 c_array_temp >> c_temp;
                                    c_same = 0;
                                    // One
                                    if (one == c_array_temp ||
                                    ) {
                                        c_same++;
                                    }
                                    // Two
                                    if (two == c_array_temp) {
                                        c_same++;
                                    }
                                    // Three
                                    if (three == c_array_temp) {
                                        c_same++;
                                    }
                                    // Four
                                    if (four == c_array_temp) {
                                        c_same++;
                                    }
                                    // Five
                                    if (five == c_array_temp) {
                                        c_sameme++;
                                    }
                                    // Six
                                    if (six == c_array_temp) {
                                        c_same++;
                                    }
                                    if (c_same > 3) {
                                        goto skip;
                                    }
                            }

// Writes the numbers to the string
 for (int numW=0; numW<6; numW++) {
                   switch(numW) {
                     			    case 0:
                                        c_string.append(one);
                                        c_string.append(" ");
                                        break;
                                    case 1:
                                        c_string.append(two);
                                        c_string.append(" ");
                                        break;
                                    case 2:
                                        c_string.append(three);
                                        c_string.append(" ");
                                        break;
                                    case 3:
                                        c_string.append(four);
                                        c_string.append(" ");
                                        break;
                                    case 4:
                                        c_string.append(five);
                                        c_string.append(" ");
                                        break;
                                    case 5:
                                        c_string.append(six);
                                        break;
                     			}
                    		}

                            // Writes the numbers to the array
                            c_array.push_back(c_string);
error: no match for 'operator<<' in c_array_temp << c_temp'|
error: no match for 'operator==' in 'one == c_array_temp'| [happens for all one - six]
error: no match for 'operator>>' in c_string >> one'|

Sorry I have tried my best to construct something but I have no luck :(

Hope you can help, Thanks Regards X

Without looking trough all of your code, there are a few syntax errors, for example:

if (one == c_array_temp ||
       ) {

Also, the variables "one", "two", "three" etc are never declared....

I think you're making it to hard on yourself. If you want to break the string into integers and store them all, how about using the code I posted earlier?
You can access all the numbers in the array in the same way you would do it with an array:

cout << all_numbers[0] ;

would give the first number stored in the vector. If you understand how you can access the vector, the only thing you would have to do is to write a "compare-algorithm"

niek_e thanks for the advice.

Its the early the hours of the morning and been trying to fix this the whole day.

I think I will sleep on it and take your advice and try something tomorrow.

Thanks, Regards X

PS: Ill keep you posted

Ok i have decided to start again and break up my problem into little problems and solves them one by one and eventually and hopefully it is complete working order!!! :)

Ok First Problem:

How do I get 6 variables and insert them into a string (seperated by spaces " ") and then it into an array?

Like This?

int a = 1;
int b = 2;
int c = 3;
int d = 4;
int e = 5;
int f = 6;

string abc;

vector<string> abcd;

abc << a << " " << b << " " << c << " " <<
            d << " " << e << " " << f;

abcd.append(abc);

Now would the above code do what I require?

Thankyou, Regards X

PS: I am trying my best!!! :(

You might use stringstream ...

#include <vector>
#include <string>
#include <sstream>
using namespace std;

int main()
{
    int a = 1;
    int b = 2;
    int c = 3;
    int d = 4;
    int e = 5;
    int f = 6;

    vector<string> abcd;
    stringstream sstrm;

    sstrm    << a << " " 
             << b << " " 
             << c << " " 
             << d << " " 
             << e << " " 
             << f;

    abcd.push_back(sstrm.str());

    return 0;
}

Note that vector has no append() member function, see vector reference

Thanks for the help BUT when I use it more than once it becomes duplicated.

Example:
1 2 3 4 5 6
use the same stringstream variable
1 2 3 4 5 62 3 4 5 6 7

Adds the the new variables ontop of the previous one

Anyway how would you clear the variable once it has been used?

Also what does the function .str() do?

Thanks, Regards X

Thanks for the help BUT when I use it more than once it becomes duplicated.

Anyway how would you clear the variable once it has been used?

You could use sstrm.str(""); which would load the stringstream with an empty string.

Also what does the function .str() do?

The function .str() (if no parameters are given) returns the value of the stringstream represented as a std::string.
If you do give a parameter, it will load the stringstream with the value given. sstrm.str("test") will set put "test" in the stringstream, clearing all other values that may have been in the stream.

Thankyou, perfect!!!

Now that I have learned how to make a string.

I would now like to learn how to break a string.

So lets break that string up into those individual variables again.

#include <vector>
#include <string>
#include <sstream>
using namespace std;

int main()
{
    int a = 0;
    int b = 0;
    int c = 0;
    int d = 0;
    int e = 0;
    int f = 0;

    vector<string> abcd;
    stringstream sstrm;
    string buffer;

   while (getline(sstrm,buffer,' '))
    {
        //convert buffer to int and put in 'integer'
        istringstream strin(buffer);
        a >> strin;
        b >> strin;
        c >> strin;
        d >> strin;
        e >> strin;
        f >> strin;
    }
}

Correct?

Thanks, Regards X

[code=cpp]
abcd.push_back(sstrm.str());

PS: Why dont we just use sstrm here instead of sstrm.str()? Normal String vs. Std String? Thanks

Correct?

No. The stringstream doesn't contain any values, so getline will fail. Look closely at your code again

PS: Why dont we just use sstrm here instead of sstrm.str()? Normal String vs. Std String? Thanks

Actually: Stringstreams versus std::string

Futhermore, you have the variables the wrong way around, i.e. instead of a >> strin; use strin >>a;

Comments
Thanks for the help
Duh... Missed that completely

I made the corrects to my code and it is working now BUT the problem is there coming out 6 0 0 0 0 0 instead of 1 2 3 4 5 6 (I gather the only first number is only be initialized from the below code).

So how would I fix it?

while (getline(sstrm,buffer,' ')) {
 //convert buffer to int and put in 'integer'
 istringstream strin(buffer);
 strin >> a;
 strin >> b;
 strin >> c;
 strin >> d;
 strin >> e;
 strin >> f;
}

Thanks, Regards X

If you want to use 6 different integers for this, then you can't use a loop.
Why don't you want to use an int-vector as I suggested in post #11 ?

That way you have a nice small code that puts all the integers in a vector, so you can use them later.

[edit]
Here's a full example :

#include <iostream>
#include <string>
#include <sstream>
#include <vector>
using namespace std;


int main()
{
    vector <int> all_numbers;
    stringstream ss;
    string input  = "1,2,3,4,5";
    string buffer;
    int integer;
    ss << input;
    while (getline(ss,buffer,','))
    {
        //convert buffer to int and put in 'integer'
        istringstream strin(buffer);
        strin >> integer;
        // integer now contains the numeric value of the buffer
        //so now push it in the vector
        all_numbers.push_back(integer);
    }
    cout << "Pushed " << all_numbers.size() << " numbers in the vector\n";
   
    /* everything is in the vector now. Time to get it out again and back in a string */

    ostringstream os;
    string new_string = "";
    for (unsigned int j = 0; j < all_numbers.size();j++)
    {
        os.str("");
        // times 11 to demonstrate that it's an int
        os << all_numbers[j]*11;  
        //put underscores in between the numbers 
        new_string += (os.str() + "_"); 
    }

    cout << "new string with underscores (and times 11): " << new_string;
    cin.get();
    return 0;
Comments
Thanks for the example, much appericated.

Thanks for the example!

Had a quick look and I will try implement it tomo morning!

To answer your question before I go to bed.
"That way you have a nice small code that puts all the integers in a vector, so you can use them later."

I would use an int but it will not allow me to store the variables as I want.

Eg. 1 2 3 4 5 6, 1 2 3 4 5 7, etc

Thanks for all your help niek_e

Give you an update in the morning, goodnight

Thanks, Regards X

The example is close to perfect for the logic I require BUT it skips the step of assigning individual variables to 1 2 3 4 5 instead its just parts of an array. Everything else works beautifuly and also for the for loop why is it "unsigned int" and not just "int"?

Thanks Again nearly there :(, Regards X

This question has already been answered. Start a new discussion instead.