Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/337.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++&;Python:格式化数组项并从中删除重复项 我把代码从Python翻译成C++(其中我不是经常的用户, 仅仅理解了一点synthax),并陷入了一个需要格式化的问题 然后从已格式化的数组中删除重复项。最后,数一数数字 数组c中的项的数目。我已经概述了有问题的部分(我留下的部分是用Python编写的)。 如您所见,我有一个包含10个数字的m_l数组,它将用于5个for循环。我有一个初步准备好的b数组,在这里我将把5中所有可能的总和用于循环构造。然后,正如前面提到的,我想格式化b数组的项,因此每个项在点后有4个数字(即128.7153)。在格式化之后,我将转移到删除重复,然后取消对c数组项计数的确认。 你能帮我做那部分吗?先谢谢你。 到目前为止,代码如下所示: #include <iostream> #include <vector> #include <bits/stdc++.h> using namespace std; int main() { double m_l[10] = {12.05132, 178.0779, 52.13106, 44.15764, 100.15764, 726.0874, 109.11398, 9.11518, 198.10388, 36.0773} vector<double> b; for (int d = 0; d < 10; d++){ for(int e = 0; e < 10; e++){ for(int f = 0; f < 10; f++){ for(int g = 0; g < 10; d++){ for(int h = 0; h < 10; h++){ b.push_back(m_l[d]+m_l[e]+m_l[f]+m_l[g]+m_l[h]);}}}}} int bSize = sizeof(b)/sizeof(b[0]); cout << "The size of the b is: " << bSize << "\n"; cout << " " << "\n"; _______________________________________________________ myformattedlist = ["%.4f" % item for item in b] c = set(myformattedlist) _______________________________________________________ int cSize = sizeof(c)/sizeof(c[0]); cout << "The size of the c is: " << cSize << "\n"; cout << " " << "\n"; return 0; } #包括 #包括 #包括 使用名称空间std; int main() { 双m_l[10]={12.05132178.0779,52.13106,44.15764,100.15764,726.0874,109.11398,9.11518, 198.10388, 36.0773} 载体b; 对于(int d=0;d_Python_C++_Arrays_Formatting - Fatal编程技术网

C++&;Python:格式化数组项并从中删除重复项 我把代码从Python翻译成C++(其中我不是经常的用户, 仅仅理解了一点synthax),并陷入了一个需要格式化的问题 然后从已格式化的数组中删除重复项。最后,数一数数字 数组c中的项的数目。我已经概述了有问题的部分(我留下的部分是用Python编写的)。 如您所见,我有一个包含10个数字的m_l数组,它将用于5个for循环。我有一个初步准备好的b数组,在这里我将把5中所有可能的总和用于循环构造。然后,正如前面提到的,我想格式化b数组的项,因此每个项在点后有4个数字(即128.7153)。在格式化之后,我将转移到删除重复,然后取消对c数组项计数的确认。 你能帮我做那部分吗?先谢谢你。 到目前为止,代码如下所示: #include <iostream> #include <vector> #include <bits/stdc++.h> using namespace std; int main() { double m_l[10] = {12.05132, 178.0779, 52.13106, 44.15764, 100.15764, 726.0874, 109.11398, 9.11518, 198.10388, 36.0773} vector<double> b; for (int d = 0; d < 10; d++){ for(int e = 0; e < 10; e++){ for(int f = 0; f < 10; f++){ for(int g = 0; g < 10; d++){ for(int h = 0; h < 10; h++){ b.push_back(m_l[d]+m_l[e]+m_l[f]+m_l[g]+m_l[h]);}}}}} int bSize = sizeof(b)/sizeof(b[0]); cout << "The size of the b is: " << bSize << "\n"; cout << " " << "\n"; _______________________________________________________ myformattedlist = ["%.4f" % item for item in b] c = set(myformattedlist) _______________________________________________________ int cSize = sizeof(c)/sizeof(c[0]); cout << "The size of the c is: " << cSize << "\n"; cout << " " << "\n"; return 0; } #包括 #包括 #包括 使用名称空间std; int main() { 双m_l[10]={12.05132178.0779,52.13106,44.15764,100.15764,726.0874,109.11398,9.11518, 198.10388, 36.0773} 载体b; 对于(int d=0;d

C++&;Python:格式化数组项并从中删除重复项 我把代码从Python翻译成C++(其中我不是经常的用户, 仅仅理解了一点synthax),并陷入了一个需要格式化的问题 然后从已格式化的数组中删除重复项。最后,数一数数字 数组c中的项的数目。我已经概述了有问题的部分(我留下的部分是用Python编写的)。 如您所见,我有一个包含10个数字的m_l数组,它将用于5个for循环。我有一个初步准备好的b数组,在这里我将把5中所有可能的总和用于循环构造。然后,正如前面提到的,我想格式化b数组的项,因此每个项在点后有4个数字(即128.7153)。在格式化之后,我将转移到删除重复,然后取消对c数组项计数的确认。 你能帮我做那部分吗?先谢谢你。 到目前为止,代码如下所示: #include <iostream> #include <vector> #include <bits/stdc++.h> using namespace std; int main() { double m_l[10] = {12.05132, 178.0779, 52.13106, 44.15764, 100.15764, 726.0874, 109.11398, 9.11518, 198.10388, 36.0773} vector<double> b; for (int d = 0; d < 10; d++){ for(int e = 0; e < 10; e++){ for(int f = 0; f < 10; f++){ for(int g = 0; g < 10; d++){ for(int h = 0; h < 10; h++){ b.push_back(m_l[d]+m_l[e]+m_l[f]+m_l[g]+m_l[h]);}}}}} int bSize = sizeof(b)/sizeof(b[0]); cout << "The size of the b is: " << bSize << "\n"; cout << " " << "\n"; _______________________________________________________ myformattedlist = ["%.4f" % item for item in b] c = set(myformattedlist) _______________________________________________________ int cSize = sizeof(c)/sizeof(c[0]); cout << "The size of the c is: " << cSize << "\n"; cout << " " << "\n"; return 0; } #包括 #包括 #包括 使用名称空间std; int main() { 双m_l[10]={12.05132178.0779,52.13106,44.15764,100.15764,726.0874,109.11398,9.11518, 198.10388, 36.0773} 载体b; 对于(int d=0;d,python,c++,arrays,formatting,Python,C++,Arrays,Formatting,如果只想删除向量中的重复项,可以执行以下操作: c++11: for (auto it = b.begin(); it != b.end(); it++) // first iterate over the vector for (auto it2 = it + 1; it2 != b.end(); it2++) // then for each value after the current, check if there is a duplicate if (*it2

如果只想删除向量中的重复项,可以执行以下操作:

c++11:
for (auto it = b.begin(); it != b.end(); it++) // first iterate over the vector
    for (auto it2 = it + 1; it2 != b.end(); it2++) // then for each value after the current, check if there is a duplicate
        if (*it2 == *it) // if it is duplicate
        {
            auto tmp = it2; // put the value of the iterator in tmp
            it2--; // decrease the iterator so we don't lose it when erasing (which can cause a segfault)
            b.erase(tmp); // then remove it
        }
c++98:
for (std::vector<double>::iterator it = b.begin(); it != b.end(); it++)
    for (std::vector<double>::iterator it2 = it + 1; it2 != b.end(); it2++)
        if (*it2 == *it)
        {
            std::vector<double>::iterator tmp = it2;
            it2--;
            b.erase(tmp);
        }
c-style:
for (int i = 0; i < b.size(); i++)
    for (int j = i + 1; j < b.size(); j++)
        if (b[j] == b[i])
        {
            b.erase(b.begin() + j);
            j--; // decrease the index so we don't skip one member
        }
c++11:
for(auto it=b.begin();it!=b.end();it++)//首先迭代向量
对于(auto it2=it+1;it2!=b.end();it2++)//然后对于当前值之后的每个值,检查是否存在重复值
if(*it2==*it)//如果它是重复的
{
auto tmp=it2;//将迭代器的值放入tmp中
it2--;//减少迭代器,以便在擦除时不会丢失它(这可能会导致segfault)
b、 擦除(tmp);//然后将其删除
}
c++98:
for(std::vector::iterator it=b.begin();it!=b.end();it++)
for(std::vector::iterator it2=it+1;it2!=b.end();it2++)
如果(*it2==*it)
{
std::vector::iterator tmp=it2;
it2--;
b、 擦除(tmp);
}
c型:
对于(int i=0;i

对于大小,只需使用member方法
std::vector::size
(在您的例子中
b.size()

您对
bSize
cSize
的计算是针对C样式数组的是
std::vector
s,它更像Python列表:有一个长度方法可以满足您的需要,不需要太多的恶作剧。这假设
b
是排序的,不是吗?不,不一定,对于每个成员,我们在所有下一个成员中检查重复项,然后删除它们,这样当我们进一步操作时,就不会有重复项之前,所以向量不需要为此排序。哦,对了,有第二个循环。或者,OP可以使用
std::set
来保留唯一的值,如果他不明确需要
std::vector
。我的代码错了,我很抱歉更正了。谢谢你的帮助。我相信,唯一剩下的是格式。H如何格式化数组b中的项目(每个项目在点后有4位数字(例如128.7153)?