【问题标题】:[Newbie]Store the element of an list in a variable (in loop)[新手]将列表的元素存储在变量中(循环中)
【发布时间】:2016-02-28 23:13:06
【问题描述】:

for (int i=0; i < n; i++) { float temp; temp = List1.(i) if(temp<0){ //do smth } }

所以我想将列表的每个下一个元素(i)存储在一个变量中,然后检查该元素是否小于 0(temp=List1..I 之后应该写什么很抱歉我的初学者问题!!!我正在使用 C++。

【问题讨论】:

  • List1的类型是什么?

标签: c++ list


【解决方案1】:

免责声明:我不使用 C++

你应该告诉我们你使用的是什么语言,在问一个你自己指出的新手问题之前,你应该先搜索一下,年轻的学徒。

如果是 java 则检查:https://docs.oracle.com/javase/tutorial/java/nutsandbolts/if.html

但在许多语言中应该这样做:

if(temp<0){
  //do something
}

对于 C++:http://www.cplusplus.com/doc/tutorial/control/

C++ 中的迭代:http://en.cppreference.com/w/cpp/language/range-for

或者如果你想使用 foreach :http://en.cppreference.com/w/cpp/algorithm/for_each

这是在 SO 上找到的答案,应该会有所帮助:https://stackoverflow.com/a/16504109/4088809

看起来像你需要的东西

for(<type> <name> : <collection>) { ... }

如果您的列表包含 int 则

for(int i : vec) {
  if(i<0){
    // do your thing
  }
}

【讨论】:

  • 那我的回答适合你还是别的什么?
  • 我知道我应该在 IF 运算符之后输入什么,我的问题是 (List1. .....) 我应该使用什么来使用来自循环。
  • 所以你的问题是关于迭代:如何迭代列表的每个元素,如果它是
  • 是的,类似于 "temp=List1.ref(i)" 但我不知道 ''List1'' 之后的运算符
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-17
  • 2015-12-22
  • 2018-12-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多