2010-11-04 119 views
1

我是C++新手,因此无法将其转换为C++。有没有例子可以看到C++数组列表的外观。我知道java有几个类可以做,但在C++中如何做到这一切都将在一个.cpp文件下。将Java转换为C++

public class Main { 
public static void main(String[] args) { 

    Inventory netFlix = new Inventory(); 

    netFlix.add("Prince of Persia",  140); 
    netFlix.add("Clash of Titans",  223); 
    netFlix.add("Avatar",    353); 
    netFlix.add("Inception",   460); 
    netFlix.add("Resident Evil",  105); 
    netFlix.add("Devil",    624); 
    netFlix.add("Memento",    117); 
    netFlix.add("D2: The Mighty Ducks", 508); 
    netFlix.add("The Lord of the Rings",910); 
    netFlix.add("The Uninvited",  120); 
+0

System.out.println(“The Terminator”) – zengr 2010-11-04 01:38:46

+2

不错的代码。还有一个问题吗? – EboMike 2010-11-04 01:39:04

+0

请按照[通用](http://tinyurl.com/so-hints)问题[准则](http://meta.stackexchange.com/q/10812),说明任何特殊的限制,显示你已经尝试过到目前为止,并询问具体是什么令你困惑。 – 2010-11-10 04:04:56

回答

0
#include <iostream> 
#include <string> 
#include <vector> 

struct Movie { 
    std::string name; 
    int score; 

    Movie(std::string name, int score) : name (name), score (score) {} 
}; 

int main() { 
    using namespace std; 
    typedef vector<Movie> Inventory; 

    Inventory movies; 
    movies.push_back(Movie("The Lord of the Rings", 910)); 
    movies.push_back(Movie("HHGTTG",     42)); 

    for (Inventory::const_iterator x = movies.begin(); x != movies.end(); ++x) { 
    cout << x->name << " = " << x->score << '\n'; 
    } 

    return 0; 
}