在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
一个基本概念: 下面一个例子加以说明: import java.util.ArrayList; public class Test { static class Person{ public Person(String n){ name = n; } @Override public String toString() { return name; } public String name; } public static void main(String[] args) { System.out.println("test"); Person p = new Person("aa"); Person p1 = new Person("bb"); ArrayList<Person> personList = new ArrayList<Test.Person>(); personList.add(p); personList.add(p1); for (Person person : personList) { System.out.println(person); //输出 aa bb } p.name = "cc"; for (Person person : personList) { System.out.println(person);//输出 cc bb } } }
上面的这个例子我们一定要明确,因为JAVA的集合是保存的对象的引用变量,而不是具体内存对象,但是C++的STL容器就不一样了, #include <iostream> #include <list> #include <iterator> #include <string> using namespace std; class Person { public: Person(string n):mName(n) { } public: string mName; }; int main() { Person p("aa"); Person p1("bb"); list<Person> personList; personList.push_back(p); personList.push_back(p1); list<Person>::iterator it ; for(it = personList.begin();it != personList.end();it++){ cout << (*it).mName << endl; //aa bb } p.mName = "cc"; for(it = personList.begin();it != personList.end();it++){ cout << (*it).mName << endl;//aa bb } return 0; }
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论