Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/124.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++ 这个程序正在崩溃,为什么? #包括 #包括 #包括 使用名称空间std; int main() { 向量arr; arr[0]。第一个=20,arr[0]。第二个=1; arr[1]。first=3,arr[1]。second=2; arr[2]。第一个=230,arr[2]。第二个=3; arr[3]。第一个=230,arr[3]。第二个=4; arr[4]。第一个=202,arr[4]。第二个=5; arr[5]。第一个=-20,arr[5]。第二个=6; 排序(arr.begin(),arr.end()); 向量::迭代器; for(it=arr.begin();it!=arr.end();it++) { cout_C++_Sorting_Vector_Stl - Fatal编程技术网

C++ 这个程序正在崩溃,为什么? #包括 #包括 #包括 使用名称空间std; int main() { 向量arr; arr[0]。第一个=20,arr[0]。第二个=1; arr[1]。first=3,arr[1]。second=2; arr[2]。第一个=230,arr[2]。第二个=3; arr[3]。第一个=230,arr[3]。第二个=4; arr[4]。第一个=202,arr[4]。第二个=5; arr[5]。第一个=-20,arr[5]。第二个=6; 排序(arr.begin(),arr.end()); 向量::迭代器; for(it=arr.begin();it!=arr.end();it++) { cout

C++ 这个程序正在崩溃,为什么? #包括 #包括 #包括 使用名称空间std; int main() { 向量arr; arr[0]。第一个=20,arr[0]。第二个=1; arr[1]。first=3,arr[1]。second=2; arr[2]。第一个=230,arr[2]。第二个=3; arr[3]。第一个=230,arr[3]。第二个=4; arr[4]。第一个=202,arr[4]。第二个=5; arr[5]。第一个=-20,arr[5]。第二个=6; 排序(arr.begin(),arr.end()); 向量::迭代器; for(it=arr.begin();it!=arr.end();it++) { cout,c++,sorting,vector,stl,C++,Sorting,Vector,Stl,分配给向量不会分配内存。 通常我们使用push_back添加具有自动内存的项目 分配。您通常这样编写代码: #include<iostream> #include<vector> #include<algorithm> using namespace std; int main() { vector<pair<int,int> > arr; arr[0].first=20,arr[0].second=1; arr

分配给
向量
不会分配内存。
通常我们使用
push_back
添加具有自动内存的项目
分配。您通常这样编写代码:

#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int main()
{
    vector<pair<int,int> > arr;
    arr[0].first=20,arr[0].second=1;
    arr[1].first=3,arr[1].second=2;
    arr[2].first=230,arr[2].second=3;
    arr[3].first=230,arr[3].second=4;
    arr[4].first=202,arr[4].second=5;
    arr[5].first=-20,arr[5].second=6;
    sort(arr.begin(),arr.end());
    vector<pair<int,int> >::iterator it;
    for(it=arr.begin();it!=arr.end();it++)
    {
        cout<<it->first<<it->second<<endl;
    }
}
arr.push_-back(对(20,1));
arr.push_back(对(3,2));
等等

但是现在使用C++11,这种编码方式已经过时了。
可以这样做(另请参见循环):

arr.push_back({20,1});
arr.push_back({3,2});
排序(arr.begin(),arr.end());
用于(自动p:arr)
{

cout与
map::operator[]
不同,
vector::operator[]
从不将新元素自动插入容器。访问不存在的元素是未定义的行为(在调试模式下,运行时可能抛出断言以帮助调试)

在C++11中,填充向量的最有效方法是:

通过初始值设定项列表:

vector<pair<int, int> > arr{ { 20, 1 }, { 3, 2 }, { 230, 3 },
{ 230, 4 }, { 202, 5 }, { -20, 6 } };
sort(arr.begin(), arr.end());
for (auto p : arr)
{
    cout << p.first << ", " << p.second << endl;
}
向量arr{ { 20, 1 }, { 3, 2 }, { 230, 3 }, { 230, 4 }, { 202, 5 }, { -20, 6 } };
或在适当位置创建条目:

  vector<pair<int, int>> arr {
    {  20, 1 }, {   3, 2 }, { 230, 3 },
    { 230, 4 }, { 202, 5 }, { -20, 6 } };
向量arr;
arr.reserve(6);//可选,只是为了提高效率
返航安置(20,1);
返航安置(3,2);
返航安置(230,3);
返航安置(230,4);
返航安置(202,5);
到达后安放(-20,6);

应该可以解决这个问题。每个问题请回答一个问题。使用默认构造函数创建向量时,向量为空。任何索引都将超出范围,并导致未定义的行为。“向量”-同样在C++11中,结束尖括号之间的空间不再是必需的。但是,
template struct d;d2)>x;
…为什么通过ctor分配内存不是“最有效”的方法?你的意思是
arr(6)
?这不仅分配内存,它还默认构造了6个元素。当然,
默认构造函数是不可操作的,这种方法更容易出错(如果不是每个元素随后都被设置,则可能会使用未初始化的值)。如果默认构造函数碰巧初始化了元素,那么这将是浪费。
vector<pair<int, int> > arr{ { 20, 1 }, { 3, 2 }, { 230, 3 },
{ 230, 4 }, { 202, 5 }, { -20, 6 } };
sort(arr.begin(), arr.end());
for (auto p : arr)
{
    cout << p.first << ", " << p.second << endl;
}
  vector<pair<int, int>> arr {
    {  20, 1 }, {   3, 2 }, { 230, 3 },
    { 230, 4 }, { 202, 5 }, { -20, 6 } };
  vector<pair<int, int>> arr;
  arr.reserve(6); // optional, is just for efficiency
  arr.emplace_back( 20, 1);
  arr.emplace_back(  3, 2);
  arr.emplace_back(230, 3);
  arr.emplace_back(230, 4);
  arr.emplace_back(202, 5);
  arr.emplace_back(-20, 6);