获取引用携带的内容

发布于 2024-11-11 15:16:50 字数 380 浏览 6 评论 0原文

我有一个对字符串对象的引用,如何从中获取数据。这是我的示例:

string key = "key1";
gpointer somepointer;

GHashTable* myTable;
g_hash_table_insert(myTable,&key1,somepointer);         

GList *keysList = g_hash_table_get_keys(myTable);// here i got keys previously set
keysList = g_list_first(keysList);
string recentKey = (keysList->data);

数据指的是字符串的引用。我如何从参考中检索数据

I have a reference to a string object how can i get the data from it. Here is my sample:

string key = "key1";
gpointer somepointer;

GHashTable* myTable;
g_hash_table_insert(myTable,&key1,somepointer);         

GList *keysList = g_hash_table_get_keys(myTable);// here i got keys previously set
keysList = g_list_first(keysList);
string recentKey = (keysList->data);

data refers to reference of a string. How can i retrieve the data from the reference

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

说谎友 2024-11-18 15:16:50

如果 keysList->datagpointer(void*),我猜想有些像
需要以下内容:

string recentKey = *(string*)keysList->data;

希望这有帮助

If keysList->data is gpointer(void*), I guess some cast like the
following is needed:

string recentKey = *(string*)keysList->data;

Hope this helps

╭⌒浅淡时光〆 2024-11-18 15:16:50

如果 data 是对字符串的引用,那么,

keysList->data 返回该字符串。

#include<iostream>
#include <cstring>
#include <string>

using namespace std;

int main()
{
    string MyString("ABCD");
    string &MyString2 = MyString;
    char * cstr;

    cout<<"\n"<<MyString;
    cout<<"\n"<<MyString2;

    cstr = new char [MyString.size()+1];
    strcpy (cstr, MyString.c_str());

    cout<<"\n"<<cstr;
    delete[] cstr; 

    return 0;
}

If data is reference to a string then,

keysList->data returns the string.

#include<iostream>
#include <cstring>
#include <string>

using namespace std;

int main()
{
    string MyString("ABCD");
    string &MyString2 = MyString;
    char * cstr;

    cout<<"\n"<<MyString;
    cout<<"\n"<<MyString2;

    cstr = new char [MyString.size()+1];
    strcpy (cstr, MyString.c_str());

    cout<<"\n"<<cstr;
    delete[] cstr; 

    return 0;
}
慕巷 2024-11-18 15:16:50

“我如何从参考文献中检索数据?”是什么意思?你有尝试过这样做吗?

std::string 中的唯一数据是字符串长度和有效负载 char* 数组。
keysList->data.length() 将访问长度,而 keysList->data.c_str() 将访问 char* 数组。

您的最后一个语句从引用中获取数据:

string recentKey = (keysList->data);

此语句创建字符串的完整副本。因为它是副本,所以对 recentKey 的任何修改都不会显示在 keysList->data 中。这是好事还是坏事取决于您的意图。

What do you mean by "How can i retrieve the data from the reference?" Have you tried just doing it?

The only data in an std::string are the string length and the payload char* array.
keysList->data.length() will access the length while keysList->data.c_str() will access the char* array.

Your last statement is getting the data from the reference:

string recentKey = (keysList->data);

This statement creates a full copy of the string. Because it is a copy, any modifications to recentKey will not show up in keysList->data. Whether that is a good or bad thing depends on your intent.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文