Feb 11, 2009 at 3:41pm UTC
for example, storing names in an array like this:
string name[ ]={ bob, mary, calvin};
is it correct?
after that how do i output ALL the 3 names? when i put:
cout << *name;
it only shows bob. sorry i am bad at array. pls help.. thanks
Feb 11, 2009 at 4:46pm UTC
thanks! but u mind helping me out further? i want to put "Ron" in front of "John" and "Bob" after "Ram".. i have been doing ages and still can't solve it.
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<string> vectA;
vector<int>::iterator it;
string name[]={"John", "Betty", "Ram"};
for (int i=0; i<3; i++)
{
vectA.push_back(name[i]);
cout << vectA[i] << " ";
}
vectA.insert(vectA.begin(), "Ron");
vectA.end();
vectA.push_back("Bob");
cout << vectA << " ";
}
Feb 11, 2009 at 4:55pm UTC
Your code is right, you just don't need the vectA.end() call.
Feb 11, 2009 at 5:14pm UTC
i dunno. it cannot run. there are a total of 26 errors when compile.
however there is a red flag at cout << vectA << " ";
Feb 11, 2009 at 5:23pm UTC
um.. ok i try.. i managed to solve it without errors using loops but do not have any iterator:
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<string> vectA;
vector<int>::iterator it;
string name[]={"John", "Betty", "Ram"};
for (int i=0; i<3; i++)
{
vectA.push_back(name[i]);
cout << vectA[i] << " ";
}
vectA.insert(vectA.begin(), "Ron");
vectA.push_back("Bob");
cout << "\n\nAfter......\n\n";
for (int i=0; i<5; i++)
{
cout << vectA[i] << " ";
}
cout << "\n\n";
}
u know how to edit mine with this:
(it=vectA.begin(); it!=vectA.end(); it++)
once again, sorry for troubling u...
Last edited on Feb 11, 2009 at 5:24pm UTC
Feb 11, 2009 at 5:26pm UTC
1 2
for ( int i=0; i<vectA.size(); i++)
//do something with vectA[i]
Is the same as
1 2
(it=vectA.begin(); it!=vectA.end(); it++)
//do something with *vectA
Last edited on Feb 11, 2009 at 5:26pm UTC
Feb 11, 2009 at 5:30pm UTC
hmm.. ok tks a lot. sorry for ur time