简体   繁体   中英

Learning C++ need help on a program assignment

So starting to learn C++. I am a pretty decent (not great) Java Programmer and I went to the C++ reference site, but my Partner and I still can't figure out what

list<int> & testSort(istream & idata, istream & sdata)

means

We got this far the list object is of type int testSort is our class

I'm having trouble with the & symbols and the istream data type.

Also, if you can explain what the vector data type is all about that would be appreciated

Well, here's some stuff to start with:

C++ Reference : This will get you started if you need to look up a standard class type.

C++ FAQ : This will help you if you get REALLY lost. Most of it is edge cases, but some is best practice.

And you already found here, which is possibly the best of all for weird cases.

But as for your specific question, remember that in C++ you don't need to have your methods in a class. There are "free functions" that are like methods, but don't belong to any class. So testSort isn't a class, or a method of a class, but a stand-alone function, much like a static method on a static class in Java.

Also, the list<> class is more like a linked list, rather than the List<> or ArrayList types of Java. The vector<> class is what you want for an array-like class in C++.

As for the & symbol, it means a reference, which you should look up in some basic C++ guides for an explanation of value types vs pointers vs references.

The istream types are streams, which I hope you are familiar with from Java. The C++ reference above has more on those in the "IOStream Library" section.

Good luck, and welcome to C/C++!

The istream & means pass by reference. References are sort of like pointers only a bit safer. I would suggest that you get a good C++ book. Since you already know basic programming and can program Java, you might like Accelerated C++ . It's a very good book and will help you pick-up idiomatic C++ quickly

& means reference in C++. It's different from a pointer, because it can't be null and you can't do pointer arithmetic on it.

Vector and list data types are part of Standard Template Library (STL). It's a standard set of data structures for C++, much like java.util Collections are for Java.

In this instance the & symbol means that idata is a reference to an object of type istream . istream is a standard type that many other types of input streams ( iostream , ifstream , etc.) derive from.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM