g_op.SetTs(" ↙↓↘←㊣→↖↑↗③■●▲/ \\\\\\//━┃〓■⌒≈¤. 。o O 〇巜 @||/\\>><<①②③∞0 1 2 3 4 5 6 7 8 9 ,TPDIETH"); c++中这种数组如何换行
时间: 2024-01-07 18:23:19 浏览: 53
在C++中,可以使用转义字符`\n`来实现数组换行。具体操作如下所示:
```cpp
#include <iostream>
using namespace std;
int main() {
char arr[] = " ↙↓↘←㊣→↖↑↗③■●▲/ \\\\\\//━┃〓■⌒≈¤. 。o O 〇巜 @||/\\>><<①②③∞0 1 2 3 4 5 6 7 8 9 ,TPDIETH";
for (int i = 0; i < sizeof(arr) - 1; i++) {
cout << arr[i];
if ((i + 1) % 10 == 0) {
cout << endl;
}
}
return ;
}
```
运行以上代码,可以实现每输出10个字符后换行一次。
相关问题
@SuppressLint("Range") public SendFrameTimeMtc selectFrameTime(long sceneStartTime) { SendFrameTimeMtc sendFrameTimeMtc = new SendFrameTimeMtc(); Cursor cursor = mRDB.query(TABLE_FRAMETIME, null, "scene_start_time=?", new String[]{String.valueOf(sceneStartTime)}, null, null, "_id", null); int batch = 0; long frameTime = 0; long curFrameTime = 0; if (cursor.getCount() > 0){ SendFrameTimeEntity sendFrameTimeEntity = new SendFrameTimeEntity(); while (cursor.moveToNext()) { if (sendFrameTimeEntity.getTs() == 0){ /** * 首次重置批次 */ frameTime = cursor.getLong(cursor.getColumnIndex("frame_time")); sendFrameTimeEntity.setTs(frameTime); batch = cursor.getInt(cursor.getColumnIndex("batch")); }else { if (batch == cursor.getInt(cursor.getColumnIndex("batch"))){ curFrameTime = cursor.getLong(cursor.getColumnIndex("frame_time")); sendFrameTimeEntity.getFtList().add(curFrameTime-frameTime); frameTime = curFrameTime; }else { // 批次不同,重置 sendFrameTimeMtc.getSendFrames().add(sendFrameTimeEntity); sendFrameTimeEntity = new SendFrameTimeEntity(); frameTime = cursor.getLong(cursor.getColumnIndex("frame_time")); sendFrameTimeEntity.setTs(frameTime); batch = cursor.getInt(cursor.getColumnIndex("batch")); } } } sendFrameTimeMtc.getSendFrames().add(sendFrameTimeEntity); } return sendFrameTimeMtc; }
这段代码看起来已经比较简洁,但是还是可以进行一些优化:
1. 可以使用 try-with-resources 语句来自动关闭 cursor,避免忘记关闭而导致的资源泄漏。
2. 可以将 cursor.getColumnIndex() 方法的调用移到循环外部,避免在循环中重复调用,提升效率。
3. 可以将重复的代码抽取成方法,提高代码的复用性和可读性。
下面是优化后的代码:
```
@SuppressLint("Range")
public SendFrameTimeMtc selectFrameTime(long sceneStartTime) {
SendFrameTimeMtc sendFrameTimeMtc = new SendFrameTimeMtc();
SendFrameTimeEntity sendFrameTimeEntity = new SendFrameTimeEntity();
try (Cursor cursor = mRDB.query(TABLE_FRAMETIME, null, "scene_start_time=?", new String[]{String.valueOf(sceneStartTime)}, null, null, "_id", null)) {
int batchIndex = cursor.getColumnIndex("batch");
int frameTimeIndex = cursor.getColumnIndex("frame_time");
while (cursor.moveToNext()) {
int batch = cursor.getInt(batchIndex);
long frameTime = cursor.getLong(frameTimeIndex);
if (sendFrameTimeEntity.getTs() == 0) {
/** 首次重置批次 */
sendFrameTimeEntity.setTs(frameTime);
sendFrameTimeEntity.setBatch(batch);
} else {
if (batch == sendFrameTimeEntity.getBatch()) {
long curFrameTime = frameTime;
sendFrameTimeEntity.getFtList().add(curFrameTime - sendFrameTimeEntity.getTs());
sendFrameTimeEntity.setTs(curFrameTime);
} else {
// 批次不同,重置
sendFrameTimeMtc.getSendFrames().add(sendFrameTimeEntity);
sendFrameTimeEntity = new SendFrameTimeEntity();
sendFrameTimeEntity.setTs(frameTime);
sendFrameTimeEntity.setBatch(batch);
}
}
}
sendFrameTimeMtc.getSendFrames().add(sendFrameTimeEntity);
}
return sendFrameTimeMtc;
}
```
注意:由于无法运行代码,以上修改可能存在错误,请仔细检查修改后的代码是否正确。
@SuppressLint("Range") public SendFrameTimeMtc selectFrameTime(long sceneStartTime) { SendFrameTimeMtc sendFrameTimeMtc = new SendFrameTimeMtc(); Cursor cursor = mRDB.query(TABLE_FRAMETIME, null, "scene_start_time=?", new String[]{String.valueOf(sceneStartTime)}, null, null, "_id", null); int batch = 0; int curBatch = 0; long frameTime = 0; long curFrameTime = 0; if (cursor.getCount() > 0) { int batchIndex = cursor.getColumnIndex("batch"); int frameTimeIndex = cursor.getColumnIndex("frame_time"); SendFrameTimeEntity sendFrameTimeEntity = new SendFrameTimeEntity(); while (cursor.moveToNext()) { curFrameTime = cursor.getLong(frameTimeIndex); curBatch = cursor.getInt(batchIndex); if (sendFrameTimeEntity.getTs() == 0) { /** * 首次重置批次 */ sendFrameTimeEntity.setTs(curFrameTime); batch = curBatch; } else { if (batch == curBatch) { sendFrameTimeEntity.getFtList().add(curFrameTime - frameTime); frameTime = curFrameTime; } else { // 批次不同,重置 sendFrameTimeMtc.getSendFrames().add(sendFrameTimeEntity); sendFrameTimeEntity = new SendFrameTimeEntity(); sendFrameTimeEntity.setTs(curFrameTime); batch = curBatch; } } } sendFrameTimeMtc.getSendFrames().add(sendFrameTimeEntity); } return sendFrameTimeMtc; }
这段代码是一个方法,用于从数据库中查询某个场景的帧时间信息,并返回一个包含该场景所有帧时间的对象 SendFrameTimeMtc。该方法首先通过传入的场景开始时间,从数据库表 TABLE_FRAMETIME 中查询该场景的所有帧时间信息。然后,它会遍历查询结果,对于每一个批次的帧时间,将其时间差添加到 SendFrameTimeEntity 对象的 FtlList 中,并将该对象加入到 SendFrameTimeMtc 对象的 SendFrames 中。如果遍历过程中发现当前帧时间和上一次查询到的帧时间不在同一个批次,则会将上一个 SendFrameTimeEntity 对象加入到 SendFrameTimeMtc 中,并重置 SendFrameTimeEntity 对象的时间戳和批次号。最后,返回 SendFrameTimeMtc 对象,其中包含了该场景所有帧时间的信息。