如何在指针中保存向量元素的详细信息? [英] How to save the detail information of elements of a vector in a pointer?
问题描述
int main(){
vector <vector<string>> reference_one_cell; /*stored all the cells of 5 reference lines */
vector < vector<string> > input_array; /*stored all the cells all 52 lines */
/*comparison part*/
std::set<std::string> stringSet;
for ( auto const& vs : reference_one_cell)
for ( auto const& item : vs )
stringSet.insert(item);
for (int f=0;f<52;f++){
for (int g=0;g<10;g++){
bool found = any_of(stringSet.begin(),
stringSet.end(),
[=](std::string const& item){return input_array[f][g] == item;});
if ( found )
{
outputFile << ",";
}
else
{
outputFile<<input_array[f][g];
}
}
}
}
对于在参考行中找到的单元格,我可以打印出,".但是对于如何使用指针或对来存储我可以返回到再次恢复原始状态.预先感谢
-
添加一个变量以跟踪找到匹配项的
reference_one_cell
索引.std::vector<std::pair<std::size_t, std::size_t>> indices;
-
一旦找到匹配项,请确保更新
indices
的内容. -
在最外面的循环结束后使用
indices
.
int main(){
...
// Variable to keep track of matching indices.
std::vector<std::pair<std::size_t, std::size_t>> indices;
...
for (int f=0;f<52;f++){
for (int g=0;g<10;g++){
bool found = any_of(stringSet.begin(),
stringSet.end(),
[=](std::string const& item){return input_array[f][g] == item;});
if ( found )
{
// Use more elaborate but explicit code.
// indices.push_back(std::pair<std::size_t, std::size_t>{f, g});
// Or use succinct but not so explicit code.
indices.push_back({f, g});
outputFile << ",";
}
else
{
outputFile<<input_array[f][g];
}
}
}
// Use indices
...
}
i am trying to compare 2 two dimensional vectors, both of which contain 10 cells inside every 52 lines. I am trying to compare taking first 5 lines as a reference and then compare all the other lines with those 5 lines and then most importantly save all the information (reference id and position of the cell) that exactly which cell of the reference cell was found similar with which cells of every lines. The cells which cant be found in the reference cell should be printed as it is. this is what i tried:
int main(){
vector <vector<string>> reference_one_cell; /*stored all the cells of 5 reference lines */
vector < vector<string> > input_array; /*stored all the cells all 52 lines */
/*comparison part*/
std::set<std::string> stringSet;
for ( auto const& vs : reference_one_cell)
for ( auto const& item : vs )
stringSet.insert(item);
for (int f=0;f<52;f++){
for (int g=0;g<10;g++){
bool found = any_of(stringSet.begin(),
stringSet.end(),
[=](std::string const& item){return input_array[f][g] == item;});
if ( found )
{
outputFile << ",";
}
else
{
outputFile<<input_array[f][g];
}
}
}
}
i am able to print out "," for the cells that were found in the reference lines. But really stuck for few days with how to use pointer or pair to store all the detail information( reference id and position in the reference lines) that i can go back to the original condition again. thanks in advance
Add a variable to keep track of the indices of
reference_one_cell
where matching items were found.std::vector<std::pair<std::size_t, std::size_t>> indices;
Make sure to update the contents of
indices
once you find a match.Use
indices
after the end of the outermost loop.
int main(){
...
// Variable to keep track of matching indices.
std::vector<std::pair<std::size_t, std::size_t>> indices;
...
for (int f=0;f<52;f++){
for (int g=0;g<10;g++){
bool found = any_of(stringSet.begin(),
stringSet.end(),
[=](std::string const& item){return input_array[f][g] == item;});
if ( found )
{
// Use more elaborate but explicit code.
// indices.push_back(std::pair<std::size_t, std::size_t>{f, g});
// Or use succinct but not so explicit code.
indices.push_back({f, g});
outputFile << ",";
}
else
{
outputFile<<input_array[f][g];
}
}
}
// Use indices
...
}
这篇关于如何在指针中保存向量元素的详细信息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!