小编itn*_*ice的帖子

使用C++ ifstream从文本文件中读取整数

我想从文本文件中读取图形邻接信息并将其存储到矢量中.

  • 该文件具有任意行数

  • 每一行都有以'\n'结尾的任意整数数

例如,

First line:
0 1 4
Second line:
1 0 4 3 2
Thrid line:
2 1 3
Fourth line:
3 1 2 4
Fifth line:
4 0 1 3
Run Code Online (Sandbox Code Playgroud)

如果我一次使用getline()读取一行,我该如何解析该行(因为每行有可变数量的整数)?

有什么建议?

c++

19
推荐指数
2
解决办法
5万
查看次数

如何使用std :: auto_ptr声明动态数组?

我试图声明一个动态int数组,如下所示:

int n;
int *pInt = new int[n];
Run Code Online (Sandbox Code Playgroud)

我可以这样做std::auto_ptr吗?

我尝试过类似的东西:

std::auto_ptr<int> pInt(new int[n]);
Run Code Online (Sandbox Code Playgroud)

但它没有编译.

我想知道我是否可以使用auto_ptr构造和如何声明动态数组.谢谢!

c++ arrays auto-ptr

4
推荐指数
1
解决办法
1804
查看次数

即时上传文件到Azure Blob

我正在尝试创建一个文件并使用CloudBlockBlob.UploadFromStreamAsync()方法将其放在blob中.

这是代码:

    private async void CreateCsvFile(int recId)
    {
        using (var csvFile = new StreamWriter())
        {
            for (int i = 1; i <= recId; ++i)
            {
                Ad ad = db.Ads.Find(i);
                if (ad != null)
                {
                    string rec = String.Format("{0}, {1}, {2}, {3}, {4}", ad.Title, ad.Category, ad.Price, ad.Description, ad.Phone);
                    csvFile.WriteLine(rec);
                }
            }

            csvFile.Flush();
            string blobName = Guid.NewGuid().ToString() + recId.ToString() + ".csv";
            CloudBlockBlob fileBlob = fileBlobContainer.GetBlockBlobReference(blobName);
            await fileBlob.UploadFromStreamAsync((Stream) csvFile);
        }
    }
Run Code Online (Sandbox Code Playgroud)

更新了新要求:

// Create a decrytor to perform the stream transform. …
Run Code Online (Sandbox Code Playgroud)

c# azure-storage-blobs

3
推荐指数
1
解决办法
4336
查看次数

对指针类型的priority_queue元素进行排序

假设我们有一个priority_queue,其中包含一堆声明如下的ListNode对象:

class ListNode {
  int val;
  ListNode *next;
public:
  explicit ListNode(int v) : val(v), next(NULL) {}
  inline bool operator<(const ListNode& rhs) const {
    return val < rhs.val;
  }
};

std::priority_queue<ListNode> pq;
Run Code Online (Sandbox Code Playgroud)

通过重写operator<方法或提供排序函子,我们可以让priority_queue按val的升序保存ListNode对象。

我的问题是,如果priority_queue保存指向ListNode类的指针,我可以对指针进行排序,以便val的指向按升序排列。我怎么做?

std::priority_queue<ListNode *> pq1;
Run Code Online (Sandbox Code Playgroud)

谢谢!

c++ stl

0
推荐指数
1
解决办法
525
查看次数

标签 统计

c++ ×3

arrays ×1

auto-ptr ×1

azure-storage-blobs ×1

c# ×1

stl ×1