声明尺寸可变的2D阵列

时间:2019-09-29 16:02:57

标签: c++

我知道您是否有一个2D数组func currentAppleLanguage() -> String { return UserDefaults.standard.stringArray(forKey: "AppleLanguages")?.first ?? "" } func setAppleLanguageTo(lang: String) { // Get the current list var languages = UserDefaults.standard.stringArray(forKey: "AppleLanguages") ?? [] // Get all locales using the specified language let matching = languages.filter { $0.hasPrefix(lang) } if matching.count > 0 { // Remove those entries from the list languages.removeAll { $0 == lang } // Add them back at the start of the list languages.insert(contentsOf: matching, at: 0) } else { // It wasn't found in the list so add it at the top languages.insert(lang, at: 0) } UserDefaults.standard.set(languages, forKey: "AppleLanguages") } ,那么必须声明array[i][j],并且您无法获得用户输入的第二个数组。有什么办法可以将二维数组的长度和高度都作为变量?

j

我尝试向用户询问两个变量,并将它们设置为数组的高度和长度,但这没有用。

cin>>i;
cin>>j;
int array[i][j];

1 个答案:

答案 0 :(得分:0)

可变长度数组不是C ++中的选项。您可能在某些编译器中将其作为支持C可变长度数组的扩展。

一种解决问题的方法是std::vector

std::vector<std::vector<int>> vec(a, (std::vector<int>(b)));

godbolt上直播