C - 将数字添加到列表中?(就像python)
C++ - Adding numbers to a list? (like Python)
我是编程的新手,但是正在尝试同时学习python和c (一个与工作有关,另一个是业余时间(。我在C 中使用以下代码进行停车传感器进行测量:
> for (i=0; i<20; i++) {
> digitalWrite(TRIG1, LOW);
> delay(2);
> digitalWrite(TRIG1, HIGH);
> delay(10);
> digitalWrite(TRIG1, LOW);
> d = pulseIn(ECHO1, HIGH);
> d = d/58;
> delay(13);
> }
这应该测量距离并将其存储在d
中。它将在500毫秒的时间内完成20次。我现在想存储这20个测量值中的每一个,并从中获得中值。
在Python中,我可以创建一个列表,然后将数字添加到其中。C 中有什么等效的吗?如果没有,建议其他哪些方法在不编写非常长的代码的情况下进行中位数?
如果您必须使用普通C( (使用整数值数组:
int d[20];
for(int i = 0; i < 20; ++i)
d[i] = measureValue();
如果可以使用STL(标准模板库(,请使用std :: vector:
#include <vector>
std::vector<int> d;
for(int i = 0; i < 20; ++i)
d.push_back(measureValue());
如果您正在为Arduino写作,则可能需要为Arduino安装标准C (请参阅https://github.com/maniacbug/standardcplusplus/standardcplusplus/blob/master/master/master/readme.md.md(
相关文章:
- 为什么开发人员将C / C ++用于嵌入式系统,而不是像Python这样的高级语言与C相比?
- 我们可以将集合的值存储在变量中吗?就像我们可以将数组的值存储在变量中一样
- 如何使用 redis-plus-plus 存储二进制数据,就像我想存储结构一样?@for_stack?
- 如何将到达图形视图右侧(末端)的QGraphicsPixmapItem移动到左侧(开始)侧(就像在贪吃蛇游戏中发生的事情
- 为什么C++中没有常量引用,就像常量指针一样?
- 我必须找到给定数组中所有数字的周期,就像有很多解决方案,但数组的大小是 10^5
- 如何将输入值拆分为4位矢量,就像将n位拆分为每部分矢量的半字节一样
- 为什么向量不支持 size 作为成员变量,就像 Java 类中的长度变量一样,而是函数 size()?
- C++代码生成核心文件,就像 gdb >> gcore 一样
- 从 boost::variant 中逐个索引获取项目,就像使用 std::variant 一样
- 线程 1 从文件读取,就像线程 2 写入同一文件一样
- 如何将值插入到 c++ boost::multiindex 集合的特定索引中,就像在 std::list 中一样
- 如何在Powershell中运行C++程序,就像CMD一样?
- 将参数传递给成员函数,就像使用 std::cout 一样
- 我想知道如何将所有数值与没有任何空格的字符串分开,就像 123we45rt75 一样
- 如何获得CPU当前速度,就像Taskmgr中的价值外交一样?下图
- C - 将数字添加到列表中?(就像python)
- 在C 中,对Pair类STL的用途是什么,就像数据类型或容器一样
- 如何在cv::dft中指定FFT长度,就像在Python numpy.FFT.rfft中一样
- 如何制作更具表现力的 Python 迭代器?就像 C++ 迭代器一样