Comparison between Java list template types and C ++ list template types

Source: Internet
Author: User

A little bit of work experience may not be correct, even if it is not correct.

 

Java list is just a container interface. Defining a list must be implemented using some implementation classes such as arraylist.

Several key points for using list:

① The add () method of list can only add objects and cannot add basic data types!

② List won't open up space by itself to copy the added object by adding (). It just saves the address of the object and searches for the object to get the desired value when it is used. Therefore, add an object to the list. After the object is modified externally, the list value also changes.

③ List allows repeated object addition, and also opens a variable storage address for repeated objects.

Import Java. util. arraylist; <br/> Import Java. util. iterator; <br/> Import Java. util. list; <br/> public class demo {<br/> Public static void main (string [] ARGs) {<br/> // todo auto-generated method stub <br/> List <int []> List = new arraylist <int []> (); <br/> for (INT I = 0; I <3; I ++) {<br/> int [] value = new int [3]; <br/> for (Int J = 0; j <2; j ++) {<br/> value [J] = J; <br/>}< br/> value [2] = I; <br/> list. add (value); <br/>}< br/> // traverses members of the List member <br/> iterator <int []> itr = List. iterator (); <br/> while (itr. hasnext () {<br/> int [] object = itr. next (); <br/> for (INT I = 0; I <object. length; I ++) {<br/> system. out. print (object [I]); <br/> system. out. print (","); <br/>}< br/> system. out. print ("/N"); <br/>}< br/>} 

 

 

We can compare the Java list with the list of the C ++ standard template library.

① Below is a piece of code I have found elsewhere. Interested readers can put forward the code for adding objects to a specific variable, add the variable, modify the variable, and print the output, we will find that the list of C ++ stores the objects pushed in, rather than the addresses stored like Java.

② The list of C ++ can also be added repeatedly.

# Include <iostream> <br/> # include <list> <br/> # include <string> <br/> # include <algorithm> <br/> using namespace STD; <br/> class employee {<br/> PRIVATE: <br/> long ID; // employee ID <br/> string name; // employee name <br/> float salary; // employee salary <br/> Public: <br/> // constructor <br/> employee (long ID, string name, float salary): ID (ID), name (name), salary (salary) {}< br/> // set (), get () of member variables () method <br/> long GETID () const {<br/> retu Rn ID; <br/>}< br/> string getname () const {<br/> return name; <br/>}< br/> float getsalary () const {<br/> return salary; <br/>}< br/> void setid (long ID) {<br/> (* This ). id = ID; <br/>}< br/> void setname (string name) {<br/> (* This ). name = Name; <br/>}< br/>}; <br/> // salary comparison <br/> bool lessthan (const employee &) {<br/> return (. getsalary () <= 8000); <br/>}< br/> bool greaterthan (const employee & A) {<br/> Return (. getsalary ()> 8000); <br/>}< br/> typedef list <employee> employee_list; <br/> typedef list <employee >:: iterator employee_it; <br/> typedef list <employee>: reverse_iterator reverse_employee_it; <br/> // The output_list function outputs all elements in the list container object. <br/> void output_list (employee_list employ) {<br/> employee_it employit; <br/> for (employit = employ. begin (); employit! = Employ. end (); employit ++) {<br/> cout <(* employit ). GETID () <'/t' <(* employee ). getname () <'/t' <br/> <(* employee ). getsalary () <Endl; <br/>}< br/> // The reverse_output_list function reversely outputs all elements in the list container object. <br/> void reverse_output_list (employee_list employ) {<br/> reverse_employee_it employit; <br/> for (employit = employ. rbegin (); employit! = Employ. rend (); employit ++) {<br/> cout <(* employit ). GETID () <'/t' <(* employee ). getname () <'/t' <br/> <(* employee ). getsalary () <Endl; <br/>}< br/> int main (INT argc, char * argv []) {<br/> employee_list employ; // create a empty queue <br/> employee_it employit; // create a random access iterator <br/> // The following four statements construct an employee object and insert it to the List container object <br/> employ. push_back (employee (100, "Zhang San", 1000 0); <br/> employ. push_back (employee (101, "", 8000); <br/> employ. push_back (employee (102, "Wang Wu", 8800); <br/> employ. push_front (employee (108, "dalang", 5000); <br/> // positively outputs all elements of the list container object <br/> output_list (employ ); <br/> cout <"---------------------------------------" <Endl; <br/> // Delete elements in the container according to the Compare predicate <br/> employ. remove_if (lessthan); <br/> output_list (employ); <br/> cout <"---------------------------- ------------- "<Endl; <br/> // reversely outputs all elements of the list container object <br/> reverse_output_list (employ ); <br/> // count and output the number of employees whose salaries are higher than 8000 <br/> int I = count_if (employ. begin (), employ. end (), greaterthan); <br/> cout <"if the salary is higher than 8000 RMB," <I <! "<Endl; <br/> return 0; <br/>}< br/> 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.