使用Visual Studio 2012编译时,此代码给我错误,但使用代码块可以 [英] This code gives me errors when compiled using visual studio 2012, but with codeblocks it is okay

查看:76
本文介绍了使用Visual Studio 2012编译时,此代码给我错误,但使用代码块可以的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
#include <vector>
#include <iterator>

#define ADDR "c:\\Users\\Library2\\Desktop\\Books record\\"

using namespace std;

int main()
{
   ifstream fin(ADDR "reportcard.csv", ios::binary);
   string line;
   int rowCount=0;
   int rowIdx=0; 

   while(getline(fin,line)){
       rowCount++;
   }

   vector<string> data[**rowCount**];//this rowCount gave me "expression must have a constant value"

   fin.clear(); 
   fin.seekg(fin.beg); 

   while(getline(fin,line)) 
   {
      stringstream ss(line);  
      string value;
      while(getline(ss,value,',')){       
         data[rowIdx].push_back(value);
      }
      rowIdx++;   
   }

   fin.close();

   int colNum;
   string colName = "LAST PERSON";
   static int it;

   for(vector<string>::iterator it = data[0].begin(); it != data[0].end(); ++it)
   {
       if ((*it)== colName)
       {
           colNum = distance(data[0].begin(),it);//distance() gave me "no instances of function templates matches argument"
           break;
       }
   }
   cout << data[1][colNum] << "\t";

   return 0;
}




  1. 我想弄清楚为什么它给了我表达式必须具有一个恒定值。

  2. 我试图找到与Visual Studio 2012中可以使用的distance()相同的另一个函数。

注意:此代码用于在 LAST PERSON列下的第一个单元格中查找和获取值。使用代码块时,此代码已经可以使用。但是我需要使用Visual Studio。

note: this code is for finding and getting the value from the first cell under a column called "LAST PERSON". This code is already okay when using codeblocks. but i need to use visual studio.

推荐答案

vector<string> data[rowCount];

是可变长度数组的声明。

is a declaration of a variable length array.

可变长度数组不是标准的C ++功能。一些编译器具有自己的语言扩展,允许使用可变长度数组。其他编译器没有这种语言扩展。

Variable length arrays is not a standard C++ feature. Some compilers have their own language extensions that allow to use variable length arrays. Other compilers do not have such a language extension.

例如,可以使用向量的向量代替数组,例如

Instead of the array you could use a vector of vectors as for example

std::vector<std::vector<std::string>> data;

在binaru模式下打开文件时要注意这一点

Pay attention to that as the file is opened in the binaru mode

ifstream fin(ADDR "reportcard.csv", ios::binary);

然后使用函数 std :: getline 在一般情况下是不正确的。

then using the function std::getline is not correct in general case.

while(getline(fin,line)){
    rowCount++;
}

这篇关于使用Visual Studio 2012编译时,此代码给我错误,但使用代码块可以的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆