热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

将iostream输入代码从C++移植到C#-PortingiostreaminputcodefromC++toC#

ThisisC++codeforreadingtracesofaddressofmainmemoryforcachememorysimulation:这是用于读取主存储

This is C++ code for reading traces of address of main memory for cache memory simulation:

这是用于读取主存储器地址跟踪的C ++代码,用于高速缓存存储器仿真:

 char hex[20];
 ifstream infile;
 infile.open(filename,ios::in);
 if(!infile) {
    cout<<"Error! File not found...";
    exit(0);
 }
 int set, tag, found;
 while(!infile.eof()) { //Reading each address from trace file
      if(base!=10) {
           infile>>hex;
           address = changebase(hex, base);
      } else {
           infile>>address;
      }
      set = (address / block_size) % no_set;
      tag  = address / (block_size * no_set);
 }

I have converted this to C# code:

我已将其转换为C#代码:

 char[] hex = new char[20];
 FileStream infile=new FileStream(filename, FileMode.Open);

 if (infile == null) {
     Console.Write("Error! File not found...");
     Environment.Exit(0);
 }
 int set;
 int tag;
 int found;
 while (!infile.CanRead) { //Reading each address from trace file
     if (@base != 10) {
         infile >> hex;
         address = changebase(hex, @base);
     } else {
         infile >> address;
     }
     set = (address / block_size) % no_set;
     tag = address / (block_size * no_set);
 }

The problem is on line infile >> hex; C# is giving syntax errors, as shift right operator cannot be applied to string operators.

问题是在线infile >> hex; C#给出语法错误,因为右移运算符不能应用于字符串运算符。

Why this is not working? I'm making a small cache hit and miss calculation project.

为什么这不起作用?我正在制作一个小缓存命中和错过计算项目。

2 个解决方案

#1


12  

To quantify what Eric means:

量化Eric意味着什么:

C++ is quite flexible in the operators that can be overloaded. It has become an "idiom" that the bitshift operators << and >> also be used for input and output. This actually makes kind of sense as it is a logical construct and the eye registers some kind of "flow" between objects.

C ++在可以重载的运算符中非常灵活。比特变换操作符<<和> >也用于输入和输出已成为一种“成语”。这实际上是有道理的,因为它是一个逻辑构造,眼睛注册对象之间的某种“流”。

In C#, you don't overload those operators. What Eric means is, you need to say explicitly, on a stream object, to write (or indeed, read) something. This means calling the methods directly.

在C#中,您不会重载这些运算符。 Eric的意思是,你需要在流对象上明确地说,写(或者确实,读)某些东西。这意味着直接调用方法。

In essence you're doing the same thing - the operator overloading is just a nice shortcut, but at the end of the day some method is going to be called - be it a nice decorative "operator overload" or a plain old function call with a name.

本质上你做的是同样的事情 - 操作符重载只是一个很好的快捷方式,但在一天结束时会调用一些方法 - 它是一个很好的装饰“操作符重载”或一个普通的旧函数调用一个名字。

So, in C++ we might write:

所以,在C ++中我们可以写:

std::cout <<"Hello" <

Whereas in C# we'd write:

而在C#中我们写道:

Console.WriteLine("Hello");

If we ignore the fact that std::cout could potentially be different from the console window (this is illustrative), the concept is exactly the same.

如果我们忽略std :: cout可能与控制台窗口不同的事实(这是说明性的),概念完全相同。

To expand on the idea of the operators, you'll also have probably come across things such as stringstream.. a class that acts like a stream for strings. It's really quite useful:

为了扩展运算符的概念,你可能也会遇到诸如stringstream这样的类,它就像一个字符串流。这真的非常有用:

std::stringstream ss;
int age = 25;
ss <<"So you must be " <

In C#, we achieve this with the StringBuilder class:

在C#中,我们使用StringBuilder类实现了这一点:

StringBuilder sb = new StringBuilder();
int age = 25;
sb.Append("So you must be ").Append(age).Append(" years old");

They both do exactly the same thing. We could also do:

他们都完全一样。我们也可以这样做:

sb.AppendFormat("So you must be {0} years old", age);

This is more akin (in my opinion) to the more C-like sprintf methods, or more recently, boost's format library.

对于更像C的sprintf方法,或者最近的boost的格式库,这更像是(在我看来)。

#2


9  

C# does not use the bizarre C++ convention that bitshifting also means stream manipulation. You'll have to actually call methods for I/O.

C#没有使用奇怪的C ++约定,即bithifting也意味着流操作。您必须实际调用I / O方法。


推荐阅读
  • 开发笔记:实验7的文件读写操作
    本文介绍了使用C++的ofstream和ifstream类进行文件读写操作的方法,包括创建文件、写入文件和读取文件的过程。同时还介绍了如何判断文件是否成功打开和关闭文件的方法。通过本文的学习,读者可以了解如何在C++中进行文件读写操作。 ... [详细]
  • 本文介绍了C#中生成随机数的三种方法,并分析了其中存在的问题。首先介绍了使用Random类生成随机数的默认方法,但在高并发情况下可能会出现重复的情况。接着通过循环生成了一系列随机数,进一步突显了这个问题。文章指出,随机数生成在任何编程语言中都是必备的功能,但Random类生成的随机数并不可靠。最后,提出了需要寻找其他可靠的随机数生成方法的建议。 ... [详细]
  • C# 7.0 新特性:基于Tuple的“多”返回值方法
    本文介绍了C# 7.0中基于Tuple的“多”返回值方法的使用。通过对C# 6.0及更早版本的做法进行回顾,提出了问题:如何使一个方法可返回多个返回值。然后详细介绍了C# 7.0中使用Tuple的写法,并给出了示例代码。最后,总结了该新特性的优点。 ... [详细]
  • 本文介绍了P1651题目的描述和要求,以及计算能搭建的塔的最大高度的方法。通过动态规划和状压技术,将问题转化为求解差值的问题,并定义了相应的状态。最终得出了计算最大高度的解法。 ... [详细]
  • Java学习笔记之面向对象编程(OOP)
    本文介绍了Java学习笔记中的面向对象编程(OOP)内容,包括OOP的三大特性(封装、继承、多态)和五大原则(单一职责原则、开放封闭原则、里式替换原则、依赖倒置原则)。通过学习OOP,可以提高代码复用性、拓展性和安全性。 ... [详细]
  • 先看官方文档TheJavaTutorialshavebeenwrittenforJDK8.Examplesandpracticesdescribedinthispagedontta ... [详细]
  • 本文介绍了OpenStack的逻辑概念以及其构成简介,包括了软件开源项目、基础设施资源管理平台、三大核心组件等内容。同时还介绍了Horizon(UI模块)等相关信息。 ... [详细]
  • 本文由编程笔记#小编整理,主要介绍了关于数论相关的知识,包括数论的算法和百度百科的链接。文章还介绍了欧几里得算法、辗转相除法、gcd、lcm和扩展欧几里得算法的使用方法。此外,文章还提到了数论在求解不定方程、模线性方程和乘法逆元方面的应用。摘要长度:184字。 ... [详细]
  • 使用eclipse创建一个Java项目的步骤
    本文介绍了使用eclipse创建一个Java项目的步骤,包括启动eclipse、选择New Project命令、在对话框中输入项目名称等。同时还介绍了Java Settings对话框中的一些选项,以及如何修改Java程序的输出目录。 ... [详细]
  • EPPlus绘制刻度线的方法及示例代码
    本文介绍了使用EPPlus绘制刻度线的方法,并提供了示例代码。通过ExcelPackage类和List对象,可以实现在Excel中绘制刻度线的功能。具体的方法和示例代码在文章中进行了详细的介绍和演示。 ... [详细]
  • 本文介绍了在Cpp中将字符串形式的数值转换为int或float等数值类型的方法,主要使用了strtol、strtod和strtoul函数。这些函数可以将以null结尾的字符串转换为long int、double或unsigned long类型的数值,且支持任意进制的字符串转换。相比之下,atoi函数只能转换十进制数值且没有错误返回。 ... [详细]
  • 在C#中,使用关键字abstract来定义抽象类和抽象方法。抽象类是一种不能被实例化的类,它只提供部分实现,但可以被其他类继承并创建实例。抽象类可以用于类、方法、属性、索引器和事件。在一个类声明中使用abstract表示该类倾向于作为其他类的基类成员被标识为抽象,或者被包含在一个抽象类中,必须由其派生类实现。本文介绍了C#中抽象类和抽象方法的基础知识,并提供了一个示例代码。 ... [详细]
  • 本文介绍了在C#中SByte类型的GetHashCode方法,该方法用于获取当前SByte实例的HashCode。给出了该方法的语法和返回值,并提供了一个示例程序演示了该方法的使用。 ... [详细]
  • 本文介绍了解决java开源项目apache commons email简单使用报错的方法,包括使用正确的JAR包和正确的代码配置,以及相关参数的设置。详细介绍了如何使用apache commons email发送邮件。 ... [详细]
  • Igotthiscode(IknowitsinSpanishIcantranslateifneeded)wheretheygivemethefunctionS ... [详细]
author-avatar
阿尼陀佛1314
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有