C++ C++;向量,内存中的变量表示,常量字符*
这段代码应该解析请求,提取URI路径,然后提取每个单独的头及其值,然后将所有提取的变量传递到C++ C++;向量,内存中的变量表示,常量字符*,c++,parsing,vector,C++,Parsing,Vector,这段代码应该解析请求,提取URI路径,然后提取每个单独的头及其值,然后将所有提取的变量传递到std::vector cva,这些变量最终将传递到std::vector nv(config.nv.push_back(std::move(cva));) 我这里没有什么问题: LI>似乎解析正确地发生了,但是我存储/传递提取变量内容的方式也不正确,这是因为误解了变量如何存储/检索以及它们在C++中的范围(很可能是因为我刚开始学习C++)或者一些我不知道的更大的问题。 我插入了一些print语句,这
std::vector cva
,这些变量最终将传递到std::vector nv(config.nv.push_back(std::move(cva));)
我这里没有什么问题:
-
<> LI>似乎解析正确地发生了,但是我存储/传递提取变量内容的方式也不正确,这是因为误解了变量如何存储/检索以及它们在C++中的范围(很可能是因为我刚开始学习C++)或者一些我不知道的更大的问题。
- 我插入了一些
语句,这些语句表明我的解析已经正确完成李>print
- 但是循环的
(在末尾)的输出与预期的不同。在这里,我遇到了另一个问题,即
,如果我保留此语句,则cva.push_back(nullptr)
循环打印第一个元素,然后退出,但是当我删除该语句时,它工作得很好。为什么?for
// I already have a reqlines vector<std::string> variable holding my requests
// each request is formed as follows
// http://<hostname>:<portnumber>/path<TAB>header1:value1<TAB>header2:value2<TAB>header3:tab3<TAB>header4:tab4<etc.....>
// Pretty much a TAB separated string with request at beginning followed by extra headers
std::vector<std::string> paths;
std::vector<std::string> extraheaders;
std::vector<std::vector<std::string>> tokenized;
int count = 0; // keeps track of each request number so I can access its corresponding extra headers from extraheaders vector
if (condition is met){
// creating which will be used to store my requests paths as well as extra headers
// This has to be a const char * vector since it will be used by an external library which requires such type
std::vector<const char *> cva;
for (auto &req : reqlines){
unsigned int pos = req.find_first_of("\t", 0);
if (pos == -1){
paths.push_back(req);
extraheaders.push_back(" ");
} else {
paths.push_back(req.substr(0, pos));
extraheaders.push_back(req.substr(pos+1, std::string::npos));
}
}
for (auto &path : paths){
cva.push_back(":path");
cva.push_back(path.c_str()); // adding the URI path into cva variable
// explode function which returns a std::vector<std::string> when passing an std::string to it
tokenized.push_back(explode(extraheaders[count], ' ')); // extracting the vector<std::string> of all extra headers
// if (tokenized[count][0].compare(" ") == 0){
// printf(" %d element is empty is skipped \n");
// }else {
for (auto &tok : tokenized[count]){ // looping through extra headers of request number "count", parsing header name/value and adding it to cva
printf(" %d tok %s\n", __LINE__, tok.c_str());
printf(" %d tok address %d\n", __LINE__, &tok);
unsigned int pos = tok.find_first_of(":", 0);
if (pos == -1 )
printf(" %d there are no headers \n", __LINE__);
else {
printf("header name: %s\n", (tok.substr(0, pos)).c_str());
printf("header value: %s\n", (tok.substr(pos+1, std::string::npos)).c_str());
cva.push_back((tok.substr(0, pos)).c_str());
cva.push_back((tok.substr(pos+1, std::string::npos)).c_str());
}
}
cva.push_back(":version"); // adding version header
cva.push_back("HTTP/1.1"); // adding version header number
cva.push_back(nullptr); // adding nullptr (which is how nv is expecting cva to be terminated)
count++;
// passing the cva content to nv
config.nv.push_back(std::move(cva));
}
// Below are the printing statement to check the values of the different variables I created and populated
// above, my problem is that the population process puts some values into my variables however printing
// the content of the those variables shows different values from what I am expecting
std::cout << " " << std::endl << std::endl;
std::cout << "Printing cva" << std::endl;
for (auto &elem : cva){
std::cout << &elem << " " <<elem << std::endl;
}
std::cout << " " << std::endl << std::endl;
std::cout << "Printing paths" << std::endl;
for (auto &path : paths){
std::cout << &path << " " << path << std::endl;
}
std::cout << " " << std::endl << std::endl;
std::cout << "Printing headers" << std::endl;
for (auto &hed : extraheaders) {
std::cout << &hed << " "<<hed << std::endl;
}
}
但实际产出是:
Printing cva
:path
path1
:path
path2
:PCC
5678 //Different - Wrong value
//Different - Cookie + value is missing
:path
path3
:PCC
5678
:path
path4
:path
path5
注意:缺少打印
标题
和路径
,因为它们是相同的“工作不正常”和“//worn”不是有用的问题描述,它们本身没有任何意义。此外,stackoverflow.com上的代码示例必须包含一个。此代码示例未满足“完整”要求。@SamVarshavchik我实际上给出了代码片段以及预期和实际输出的输出示例。这个问题可能是更概念性的,即对C++中变量的理解,它们如何在内存中处理以及它们的范围,生存期。这可能只是一个小的调整来修复它,但也可能需要更高级的更改,这也是我再次尝试了解的:这个代码示例没有满足“”的“完整”要求。你知道“完整”是什么意思吗?这意味着您可以获取代码,编译并运行它,以便重现问题。这里不是这样。@SamVarshavchik我同意你的看法,代码不完整,任何人都不能编译/运行。因此,我已使其运行(超出其更大的上下文),但我无法更新此线程,因为有人编辑了它,我似乎失去了对它的控制。“工作不正常”和“//worn”不是有用的问题描述,它们本身没有任何意义。此外,stackoverflow.com上的代码示例必须包含一个。此代码示例未满足“完整”要求。@SamVarshavchik我实际上给出了代码片段以及预期和实际输出的输出示例。这个问题可能是更概念性的,即对C++中变量的理解,它们如何在内存中处理以及它们的范围,生存期。这可能只是一个小的调整来修复它,但也可能需要更高级的更改,这也是我再次尝试了解的:这个代码示例没有满足“”的“完整”要求。你知道“完整”是什么意思吗?这意味着您可以获取代码,编译并运行它,以便重现问题。这里不是这样。@SamVarshavchik我同意你的看法,代码不完整,任何人都不能编译/运行。因此,我已经使它运行(其更大的背景),但我不能更新这个线程,因为有人编辑,似乎我失去了对它的控制。
Printing cva
:path
path1
:path
path2
:PCC
1234
:Cookie
dsgfshg
:path
path3
:PCC
5678
:path
path4
:path
path5
Printing cva
:path
path1
:path
path2
:PCC
5678 //Different - Wrong value
//Different - Cookie + value is missing
:path
path3
:PCC
5678
:path
path4
:path
path5