strText就是你的CString// The pointer to my list view control. extern CListCtrl* pmyListCtrl;CString strText; int nColumnCount = pmyListCtrl->GetHeaderCtrl()->GetItemCount();// Insert 10 items in the list view control. for (int i=0;i < 10;i++) { strText.Format(TEXT("item %d"), i); // Insert the item, select every other item. pmyListCtrl->InsertItem( LVIF_TEXT|LVIF_STATE, i, strText, (i%2)==0 ? LVIS_SELECTED : 0, LVIS_SELECTED, 0, 0); // Initialize the text of the subitems. for (int j=1;j < nColumnCount;j++) { strText.Format(TEXT("sub-item %d %d"), i, j); pmyListCtrl->SetItemText(i, j, strText); } }
为什么不把你的代码贴一下,难道别人为了给你回,还要新建一个工程来把代码实现一下?
extern CListCtrl* pmyListCtrl;CString strText;
int nColumnCount = pmyListCtrl->GetHeaderCtrl()->GetItemCount();// Insert 10 items in the list view control.
for (int i=0;i < 10;i++)
{
strText.Format(TEXT("item %d"), i); // Insert the item, select every other item.
pmyListCtrl->InsertItem(
LVIF_TEXT|LVIF_STATE, i, strText,
(i%2)==0 ? LVIS_SELECTED : 0, LVIS_SELECTED,
0, 0); // Initialize the text of the subitems.
for (int j=1;j < nColumnCount;j++)
{
strText.Format(TEXT("sub-item %d %d"), i, j);
pmyListCtrl->SetItemText(i, j, strText);
}
}
代码如下:
//item for every line.
std::string item; while( rs->next() )
{
item = rs->getString("username");
usernames->InsertItem( 0, item.c_str() ); //empty.
item.clear();
}
但是item = rs->getString("username");会出错。因为rs->getString("username");返回的是const string。于是问题就出现了。
因为SetItemText参数是接收const char* ,而我可以获取的只是const string
如果是从数据库里面数据放在list里面,我也推荐使用虚表。当然你不需要排序功能的前提下。
如何转换呢?
string::c_str传递给const char*类型也是没问题的。
不清楚你遇到的问题到底是在哪里?
usernames->InsertItem( 0, item.c_str() );行不行?
如果不行,我估计是因为你的工程是unicode的问题,则如下:
const std::string item = rs->getString("username");
usernames->InsertItem( 0, CString(item.c_str()) );
这个是调用 operator=函数,参数是const string&,item本身不是const的。
string复制时不会翻倍使用内存,相同的串只保留一份。不过你还是应该用virtual list
你数据再多,也是一个循环读,所以永远只有一个std::string,比如:for (auto i = 0; i < 100000; ++i)
{
std::string = itoa(i);
}你觉得上面有多少个std::string?