Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site
Search in forums












SourceForge.net Logo
Home » U++ Library support » U++ Core » How to do interation on Vector<String>
How to do interation on Vector<String> [message #10633] Fri, 20 July 2007 09:05 Go to next message
johnevans77 is currently offline  johnevans77
Messages: 38
Registered: July 2007
Member
Dear all,

I followed tutorial on Container. Tried Vector<int> and worked. No problem.

Then i try to use Vector<String>. But, why this iteration code produce error message?

Vector<String> v2;

v2.Add("Str1");
v2.Add("Str2");
v2.Add("Str3");


for (Vector<String>::Iterator b = v.Begin(), e = v.End(); b != e; b++)
{
std::cout << "member " << " : " << *b << std::endl;
}

Please advise,
JE
Re: How to do interation on Vector<String> [message #10637 is a reply to message #10633] Fri, 20 July 2007 10:12 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
Replace "v2" for "v" in the for statement...

Anyway, iterators are just "std::" compatibility thing. For normal code, I suggest to use indicies - simpler syntax and less oportunity for errors.


Re: How to do interation on Vector<String> [message #10647 is a reply to message #10637] Fri, 20 July 2007 13:20 Go to previous messageGo to next message
johnevans77 is currently offline  johnevans77
Messages: 38
Registered: July 2007
Member
Sorry again for another careless question Smile

And, thanks for the suggestion Smile

JE
Re: How to do interation on Vector<String> [message #10649 is a reply to message #10633] Fri, 20 July 2007 14:06 Go to previous messageGo to next message
johnevans77 is currently offline  johnevans77
Messages: 38
Registered: July 2007
Member
I have revised the code:

Vector<String> v2;

v2.Add("Str1");
v2.Add("Str2");
v2.Add("Str3");


for (Vector<String>::Iterator b = v2.Begin(), e = v2.End(); b != e; b++)
{
std::cout << "member " << " : " << *b << std::endl;
}

Compiles OK, but i have this strange output:


member : 0x80cafb0
member : 0x80cafb4
member : 0x80cafb8


Not, Str1, Str2, Str3 as expected.

UPDATED:
and now it behave strange. I compile it second time and many errors are produced. I am using Slackware 11, with GCC version 3.4.6.

Thanks
JE

[Updated on: Fri, 20 July 2007 14:20]

Report message to a moderator

Re: How to do interation on Vector<String> [message #10652 is a reply to message #10649] Fri, 20 July 2007 15:20 Go to previous messageGo to next message
fudadmin is currently offline  fudadmin
Messages: 1321
Registered: November 2005
Location: Kaunas, Lithuania
Ultimate Contributor
Administrator
johnevans77, my general advice for you would be:
1. move your way of thinking from abstract to concrete;
2. start coding your real project;
3. throw away all C++ books related to std:: ;
4. try to use simple things first, if that doesn't work - ask questions here...
e.g.
String out;
Vector<String> vs;

vs.Add("Str1");
vs.Add("Str2");
vs.Add("Str3");

for (int i=0; i<vs.GetCount(); i++) {
  out<<vs[i]<<"\n";  //or similar
}
// output out somewhere...
Re: How to do interation on Vector<String> [message #10653 is a reply to message #10652] Sat, 21 July 2007 07:48 Go to previous messageGo to next message
johnevans77 is currently offline  johnevans77
Messages: 38
Registered: July 2007
Member
Thank you Smile

I will follow you advise.

Regarding to your code above.

It works. But i usually using cout to output something to console. When i try to use std::cout, the compilation produced many errors.

I can forget std namespace if there is alternative. In this case, how to output the 'out' variable to console?

Thank you,
JE
Re: How to do interation on Vector<String> [message #10654 is a reply to message #10653] Sat, 21 July 2007 12:25 Go to previous messageGo to next message
fudadmin is currently offline  fudadmin
Messages: 1321
Registered: November 2005
Location: Kaunas, Lithuania
Ultimate Contributor
Administrator
johnevans77 wrote on Sat, 21 July 2007 06:48


...
1. But i usually using cout to output something to console.


Waste of time, very bad and stupid Smile. Don't be insulted it's not at you. It's at a bit of a joke at the habit from pre-historic (pre-GUI) computers age...

Seriously, Why?
1. Is your apps interface mostly console?
2. The result is temporary:
3. you don't have "written" proof to show to other people Smile
4. can't compare to the other tests or runs.
5. can't have history.
6. have to wait while console opens
7. have to close the console window.
8. can't use that piece of code in real app.
9. ...

Strong advice:
1. do tests with your real small GUI's (or one for that purpose)
2. OR/AND learn and use ingenious U++ logging system!!!
3. AND/OR create a file, include it into the pr-aux package and do outputs to it (or use "always awake" editor like ADIE)

johnevans77 wrote on Sat, 21 July 2007 06:48


2. When i try to use std::cout, the compilation produced many errors.


Does any other console apps from examples work?
Have you setup your console package correctly (CONSOLE macro & CONSOLE flag) ?

johnevans77 wrote on Sat, 21 July 2007 06:48


3. I can forget std namespace if there is alternative.


I didn't say forget... but the alternative is very clear Smile

johnevans77 wrote on Sat, 21 July 2007 06:48


4.In this case, how to output the 'out' variable to console?


...
anyone with a clever answer?

P.S. to be continued (have to go..)

[Updated on: Sat, 21 July 2007 12:25]

Report message to a moderator

Re: How to do interation on Vector<String> [message #10655 is a reply to message #10654] Sat, 21 July 2007 13:02 Go to previous messageGo to next message
johnevans77 is currently offline  johnevans77
Messages: 38
Registered: July 2007
Member
[quote title=fudadmin wrote on Sat, 21 July 2007 12:25]
johnevans77 wrote on Sat, 21 July 2007 06:48


...
1. But i usually using cout to output something to console.


Quote:

Waste of time, very bad and stupid Smile. Don't be insulted it's not at you. It's at a bit of a joke at the habit from pre-historic (pre-GUI) computers age...

No problem Smile Hehe.

Quote:


Seriously, Why?
1. Is your apps interface mostly console?
2. The result is temporary:
3. you don't have "written" proof to show to other people Smile
4. can't compare to the other tests or runs.
5. can't have history.
6. have to wait while console opens
7. have to close the console window.
8. can't use that piece of code in real app.
9. ...


Sometimes, i usually print variable's value in console, when running in debug mode. So, i need this feature Smile When console is not open, should there is no problem. Just nothing is printed.

Quote:


Strong advice:
1. do tests with your real small GUI's (or one for that purpose)
2. OR/AND learn and use ingenious U++ logging system!!!
3. AND/OR create a file, include it into the pr-aux package and do outputs to it (or use "always awake" editor like ADIE)


what is logging function/macro i can use?

Quote:


johnevans77 wrote on Sat, 21 July 2007 06:48


2. When i try to use std::cout, the compilation produced many errors.


Does any other console apps from examples work?
Have you setup your console package correctly (CONSOLE macro & CONSOLE flag) ?


yes. All examples are working. Using vector<int>, std::cout also work. But not with vector<String>.

Quote:


johnevans77 wrote on Sat, 21 July 2007 06:48


3. I can forget std namespace if there is alternative.


I didn't say forget... but the alternative is very clear Smile


can you tell me what are the alternatives? I am very new to ultimate++.

Re: How to do interation on Vector<String> [message #10656 is a reply to message #10655] Sat, 21 July 2007 14:33 Go to previous messageGo to next message
fudadmin is currently offline  fudadmin
Messages: 1321
Registered: November 2005
Location: Kaunas, Lithuania
Ultimate Contributor
Administrator
johnevans77 wrote on Sat, 21 July 2007 12:02


...
In this case, how to output the 'out' variable to console?
...
yes. All examples are working.
...



then you HAD to notice e.g in CParser package:
Cout() << "Expresion: ";

Re: How to do interation on Vector<String> [message #10658 is a reply to message #10653] Sat, 21 July 2007 15:11 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
johnevans77 wrote on Sat, 21 July 2007 01:48

Thank you Smile

I will follow you advise.

Regarding to your code above.

It works. But i usually using cout to output something to console. When i try to use std::cout, the compilation produced many errors.

I can forget std namespace if there is alternative. In this case, how to output the 'out' variable to console?

Thank you,
JE


Well, I think the reason is that there is no

operator<<(ostream&, String)

Before going into the details, you can easily fix that by using Cout() (U++ thing) instead of std::cout.

Cout() << " member is " << *b;

Mirek
Re: How to do interation on Vector<String> [message #10660 is a reply to message #10658] Sat, 21 July 2007 17:30 Go to previous messageGo to next message
johnevans77 is currently offline  johnevans77
Messages: 38
Registered: July 2007
Member
Really big thanks, Fudadmin and Mirek Smile

If U++ has replacement for std::cout with Cout(), do we also have replacement for std::cin and std::endl?

And, where can i get documentation of that so i am not bothering you again any longer Razz

JE
Re: How to do interation on Vector<String> [message #10664 is a reply to message #10660] Sat, 21 July 2007 17:48 Go to previous messageGo to next message
fudadmin is currently offline  fudadmin
Messages: 1321
Registered: November 2005
Location: Kaunas, Lithuania
Ultimate Contributor
Administrator
johnevans77 wrote on Sat, 21 July 2007 16:30

Really big thanks, Fudadmin and Mirek Smile

If U++ has replacement for std::cout with Cout(), do we also have replacement for std::cin and std::endl?

And, where can i get documentation of that so i am not bothering you again any longer Razz

JE


Don't worry about "bothering" Smile but ...
the next line (61) after that one I mentioned in CParser reference package:
	Cout() << "Expresion: ";
		String l = ReadStdIn();

and for std::endl is ok to use '\n' in most cases, I think.
And, have you tried to press F1?

[Updated on: Sat, 21 July 2007 17:53]

Report message to a moderator

Re: How to do interation on Vector<String> [message #10665 is a reply to message #10664] Sat, 21 July 2007 18:05 Go to previous messageGo to next message
johnevans77 is currently offline  johnevans77
Messages: 38
Registered: July 2007
Member
Thanks Fudadmin Smile

Yes, i have pressed F1 Smile But I have to get my self common to TheIDE.

Thanks for ReadStdin() Smile I found it in Core -> Stream Utilities from F1 Help.

And, for \n, is it cross platform? Please correct me if i am wrong, but i think it would be different on Windows and Linux.

JE
Re: How to do interation on Vector<String> [message #10672 is a reply to message #10665] Sat, 21 July 2007 19:21 Go to previous messageGo to next message
fudadmin is currently offline  fudadmin
Messages: 1321
Registered: November 2005
Location: Kaunas, Lithuania
Ultimate Contributor
Administrator
johnevans77 wrote on Sat, 21 July 2007 17:05


...
And, for \n, is it cross platform? Please correct me if i am wrong, but i think it would be different on Windows and Linux.

JE

About CR -you should read this topic:
http://www.ultimatepp.org/forum/index.php?t=msg&th=2525& amp;start=0&
Re: How to do interation on Vector<String> [message #10675 is a reply to message #10672] Sat, 21 July 2007 19:49 Go to previous message
johnevans77 is currently offline  johnevans77
Messages: 38
Registered: July 2007
Member
Thanks Fudadmin Smile

Thats clear.

JE
Previous Topic: Problem using Split on carriage return
Next Topic: Can I extend the Serialization to other types?
Goto Forum:
  


Current Time: Fri Mar 29 15:09:11 CET 2024

Total time taken to generate the page: 0.01812 seconds