Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/146.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++ 以C+;中的特定格式打印从文本文件获得的坐标向量+;_C++_Vector_Convex Hull - Fatal编程技术网

C++ 以C+;中的特定格式打印从文本文件获得的坐标向量+;

C++ 以C+;中的特定格式打印从文本文件获得的坐标向量+;,c++,vector,convex-hull,C++,Vector,Convex Hull,以下是我正在研究的代码- #include <iostream> #include <algorithm> #include <vector> #include <fstream> #include <iterator> using namespace std; struct coord { long x,y; }; int main() { ifstream nos("numbers.txt"); vector< long

以下是我正在研究的代码-

#include <iostream>
#include <algorithm>
#include <vector>
#include <fstream>
#include <iterator>

using namespace std;

struct coord {
long x,y;
};

int main()
{
ifstream nos("numbers.txt");
vector< long > values;
double val;
while ( nos >> val )
{
    values.push_back(val);
}

copy(values.begin(), values.end(), ostream_iterator<double>(cout, "\n" ));
return 0;

}
然后,我使用我的程序,将这些数字输入到一个向量中,并以相同的格式打印出该向量

谁能告诉我这样做的正确方法是什么

我已经参考了代码的,但我需要阅读和打印在上面提到的格式,我不知道什么是最好的方式继续


为了更清晰,我正在尝试实现一个凸包算法。我试图在同一时间更好地编程,因此这是一个飞跃

问题:你为什么要把
混在一起?我假设您希望在整个代码中使用
long
。最简单的方法是在数字之间添加一个伪变量,读取

int main()
{
ifstream nos("numbers.txt");
vector< long > values;
long val1, val2;
char dummy;
while ( nos >> val1 >> dummy >> val2)
{
    values.push_back(val1);
    values.push_back(val2);
}

copy(values.begin(), values.end(), ostream_iterator<long>(cout, "\n" ));
}
此外,在C++11中,您可以将代码更改为:

long x, y;
char dummy;
while ( nos >> x >> dummy >> y )
{
    values.emplace_back(coord{x, y});
}

或者您也可以查看
std::pair
来放置坐标。

对于这样简单的操作可能有点过分,但我会分别重载ostream和istream输出和输入操作符

编辑:我想因为结构有默认的公共变量,所以您不需要friend类,但我会保留它,因为这是重载的常见做法>

struct-coord{
长x,y;
友人级奥斯特雷姆;
朋友类是梦想;
};
istream&operator>>(istream&is、coord&c)
{
字符逗号;
返回值为>>c.x>>逗号>>c.y;
}

ostream&operator如果要读取一个int,请忽略逗号,然后是int,每行一对?这正是您要处理的格式吗?在一部分中,您说
以相同的格式打印该向量
,然后您说
我需要以特定的格式读取并打印。它是哪一个?我最初只是尝试使用struct。后来我发现了我在帖子中提到的链接,并添加了这段代码。谢谢你为我澄清这一点!
std::ostream& operator<<(std::ostream& os, const coord& c)
{
    os << c.x << " " << c.y;
    return os;
}

int main()
{
ifstream nos("numbers.txt");
vector< coord > values;
coord c;
char dummy;
while ( nos >> c.x >> dummy >> c.y )
{
    values.push_back(c);
}

copy(values.begin(), values.end(), ostream_iterator<coord>(cout, "\n" ));
}
long x, y;
char dummy;
while ( nos >> x >> dummy >> y )
{
    values.emplace_back(coord{x, y});
}
struct coord {
    long x,y;

    friend class ostream;
    friend class istream;
};

istream& operator>>( istream& is, coord& c )
{
     char comma;
     return is >> c.x >> comma >> c.y;
}

ostream& operator<<( ostream& os, const coord& c )
{
     char comma = ',';
     return os << c.x << comma << c.y;
}

int main()
{
    ifstream nos("numbers.txt");
    vector< coord > values;

    coord val;
    while ( nos >> val )
        values.push_back(val);

    for each( value : values )
        cout << value << endl;

    return 0;
}