如何在 for 循环中声明和使用结构数组?
How do I declare and use an array of struct in a for loop?
我正在使用 cpp 和 sqlite http://github.com/jacksiro/vsongbook-cpp 使用 falcon c++ IDE 开发本机 winapi 应用程序。这是我的数据库:
void CreateDatabase()
{
sqlite3 * db = NULL;
int db_qry;
const char * sqlCreateTables =
"CREATE TABLE my_friends(friend_name VARCHAR(20), "
"friend_job VARCHAR(20), friend_job INTEGER(11));";
db_qry = sqlite3_open("friends.db", &db);
db_qry = sqlite3_exec(db, sqlCreateTables, NULL, NULL, NULL);
sqlite3_close(db);
}
我用这个插入它的数据库:
void InsertToDatabase()
{
int db_qry;
char *error;
sqlite3 *db;
const char *sqlInsert =
"INSERT INTO my_friends(friend_name, friend_job, friend_age) "
"VALUES('Ngunjiri James', 'Teacher', 25);"
"INSERT INTO my_friends(friend_name, friend_job, friend_age) "
"VALUES('Wafula Shem', 'Capernter', 30);"
"INSERT INTO my_friends(friend_name, friend_job, friend_age) "
"VALUES('Jane Akinyi', 'Nurse', 23);";
db_qry = sqlite3_open("friends.db", &db);
db_qry = sqlite3_exec(db, sqlInsert, NULL, NULL, &error);
sqlite3_close(db);
}
现在,我的窗口中有一个简单的列表框,我在其中使用sqlite表中的值填充它,如下所示。
inline UINT AddStringList(const HWND hList,const ustring& s)
{
return static_cast<UINT>(SendMessage(hList,LB_ADDSTRING,0,
reinterpret_cast<LPARAM>(s.c_str())));
}
//I call this method in my WM_CREATE in WinMain and it works very well
void AddFriendListBox(const HWND hList)
{
sqlite3 * db = NULL;
sqlite3_stmt * stmt;
int i, db_qry;
const char * tail;
const char * sqlSelect = "SELECT * FROM my_friends";
db_qry = sqlite3_open("friends.db", &db);
if(sqlite3_prepare(db, sqlSelect, -1, &stmt, &tail) == SQLITE_OK)
{
while(sqlite3_step(stmt) != SQLITE_DONE)
{
for(i = 0; i < sqlite3_column_count(stmt); i++) {
const unsigned char * p = reinterpret_cast<const unsigned char *>
(sqlite3_column_text(stmt, 0));
const char * finaltext = (const char *)p;
AddStringList(hList,_T(finaltext));
}
}
sqlite3_finalize(stmt);
}
sqlite3_close(db);
}
我遇到了一些关于创建列表框并使用struct
填充它的代码 http://zetcode.com/gui/winapi/advancedcontrols/:
typedef struct {
wchar_t name[30];
wchar_t job[20];
int age;
} Friends;
Friends friends[] = {
{L"Lucy", L"waitress", 18},
{L"Thomas", L"programmer", 25},
{L"George", L"police officer", 26},
{L"Michael", L"producer", 38},
{L"Jane", L"steward", 28},
};
然后按如下方式使用它:
case WM_CREATE:
hwndList = CreateWindowW(WC_LISTBOXW , NULL, WS_CHILD
| WS_VISIBLE | LBS_NOTIFY, 10, 10, 150, 120, hwnd,
(HMENU) IDC_LIST, NULL, NULL);
for (int i = 0; i < ARRAYSIZE(friends); i++) {
SendMessageW(hwndList, LB_ADDSTRING, 0, (LPARAM) friends[i].name);
}
break;
因为我无法理解上面使用ARRAYSIZE
。我用int
5 代替它,如下所示:
case WM_CREATE:
hwndList = CreateWindowW(WC_LISTBOXW , NULL, WS_CHILD
| WS_VISIBLE | LBS_NOTIFY, 10, 10, 150, 120, hwnd,
(HMENU) IDC_LIST, NULL, NULL);
for (int i = 0; i < 5; i++) {
SendMessageW(hwndList, LB_ADDSTRING, 0, (LPARAM) friends[i].name);
}
break;
好了,现在说了又做。
如何创建一个类似于上述朋友的
struct
,并使用for
循环用我的 sqlite3 表中的值填充它?
我仍然是 c/c++ 的新手。
由于我无法忍受上面使用的"ARRAYSIZE",我用 int 5 替换了它,如下所示
它很可能是一个宏,可以定义为:
#define ARRAYSIZE(array) (sizeof(array)/sizeof(array[0]))
这对于在使用宏的同一函数中定义的数组非常有效。当数组传递给函数时,它将不起作用。在获取数组作为参数的函数中,sizeof(array)
的计算结果将是指针的大小,而不是数组的大小。
例如
void foo(int array[])
{
int s = ARRAYSIZE(array); // Won't work here.
}
void foo()
{
int array[] = {1, 3, 20};
int s = ARRAYSIZE(array); // Will work here.
}
如果有选择,则应使用std::vector
.从长远来看,这将使编码变得容易得多。
在 C++11 中,您可以将friends
定义为:
std::vector<Friends> friends = {
{L"Lucy", L"waitress", 18},
{L"Thomas", L"programmer", 25},
{L"George", L"police officer", 26},
{L"Michael", L"producer", 38},
{L"Jane", L"steward", 28},
};
相关文章:
- C++将文本文件中的数据读取到结构数组中
- JSON转换为nlohmann JSON-lib中的结构数组
- 需要帮助将结构数组传递给函数
- 在 64 位边界上对齐C++结构数组?
- 带有枚举方向/类型的气泡排序结构数组
- 在C++中访问结构数组中的数据
- 从指向结构数组的指针中提取元素
- 如何制作一个地图,其中的值是C++中的结构数组
- 如何在函数中显示结构数组
- 包含作为成员C++函数的结构数组
- 如何修复函数中的 fstream 文件输入以将正确的信息存储在结构数组中?
- std::transform 将一个结构数组复制到另一个结构数组
- 将非托管结构数组封送到托管数组
- 显示结构数组
- 结构数组中所有类似字段的总和
- 结构数组的构造函数错误,错误消息:没有构造函数实例与参数列表匹配
- 在 glsl opengl 中将嵌套结构数组作为统一传递
- 使用初始化列表填充C++中的多维结构数组时出现问题
- 如何将字符串字符与结构数组进行比较?
- C++结构数组