我想问你应该如何将这段代码看成C++

ram*_*ein 3 php c++

我想问你这个代码应该如何看待C++:

<?php
for ($i = 1; $i <= 10; $i++) {
    $array[$i]="test".$i;
}
?>
Run Code Online (Sandbox Code Playgroud)

eq-*_*eq- 6

它看起来像这样(一个完整的程序).

/* required headers */
#include <map>
#include <cstdlib>

/* code has to be inside a function; main is the start-point of the program */
int main() {
  std::map<int, int> array;
  for (int i = 1; i <= 10; ++i) {
    array[i] = i;
  }
  return EXIT_SUCCESS;
}
Run Code Online (Sandbox Code Playgroud)

我使用上面的地图,因为PHP"数组"实际上就像其他语言中的地图(虽然完全模仿他们在静态类型语言中的行为是一件麻烦事).当然,由于程序没有什么作用,你可以节省一些打字而不是输入有效无效的东西.

编辑:

/* required headers */
#include <map>
#include <string>
#include <sstream>
#include <cstdlib>

/* code has to be inside a function; main is the start-point of the program */
int main() {
  std::map<int, std::string> array;
  for (int i = 1; i <= 10; ++i) {
    std::ostringstream stream;
    stream << "test" << i;
    array[i] = stream.str();
  }
  return EXIT_SUCCESS;
}
Run Code Online (Sandbox Code Playgroud)