如何计算包括结尾新行的行?

时间:2018-04-23 23:58:27

标签: c++11

我一直在网上搜索很长时间,据我所知,没有正确答案。现在最常见的答案如下所示:

import android.app.Activity;
import android.app.FragmentManager;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
public class MainActivity extends FragmentActivity implements communicator{
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    @Override
    public void respond(int i) {
        android.support.v4.app.FragmentManager fm=getSupportFragmentManager();
        FragmentB f2= (FragmentB)fm.findFragmentById(R.id.fragment2);
        f2.changedata(i);
    }
}

main_activity.xml

<?xml version="1.0" encoding="utf-8"?>
<android.widget.LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#00BBFF"
    android:orientation="vertical"
    tools:context="com.example.dhanya.myfragmentexample.MainActivity">
    <fragment
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/fragment"
        android:name="com.example.dhanya.myfragmentexample.FragmentA"
        />
    <fragment
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/fragment2"
        android:name="com.example.dhanya.myfragmentexample.FragmentB"
        />
</android.widget.LinearLayout>










fragmentA.java

package com.example.dhanya.myfragmentexample;
import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class FragmentA extends Fragment implements AdapterView.OnItemClickListener{
    ListView list;
    communicator comm;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_, container, false);
    }
    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        comm= (communicator) getActivity();
        list=(ListView)getActivity().findViewById(R.id.lv);
        ArrayAdapter ad=ArrayAdapter.createFromResource(getActivity(),R.array.listval,android.R.layout.simple_list_item_1);
        list.setAdapter(ad);
        list.setOnItemClickListener(this);
           }
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        TextView txt=(TextView)view;
        String str=txt.getText().toString();
        comm.respond(position);
    }
}






























fragmentA.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.dhanya.myfragmentexample.FragmentA"
    android:background="#FFBB00">
    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/lv"/>
</FrameLayout>







fragmentB.java
package com.example.dhanya.myfragmentexample;
import android.content.Context;
import android.content.res.Resources;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class FragmentB extends Fragment {
TextView txt;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_fragment_b, container, false);
    }
    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        txt=(TextView)getActivity().findViewById(R.id.textv);
    }
    public void changedata(int i)
    {
        Resources res=getResources();
        String[] des=res.getStringArray(R.array.desciptions);
       // String str=Integer.toString(i);
        txt.setText(des[i]);
       // txt.setText(str);    }}
fragmentB.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.dhanya.myfragmentexample.FragmentB"
    android:background="#99CC00"
    >
    <!-- TODO: Update blank fragment layout -->
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/textv"/>
</FrameLayout>



strings.xml

<resources>
    <string name="app_name">MyFragmentExample</string>
    <!-- TODO: Remove or change this placeholder text -->
    <string name="hello_blank_fragment">Hello blank fragment</string>
    <string-array name="listval">
        <item>fragments</item>
        <item>Activities</item>
        <item>Services</item>
        <item>Content Providers</item>
    </string-array>
    <string-array name="desciptions">
        <item>A Fragment represents a behavior or a portion of user interface in an Activity. You can combine multiple fragments in a single activity to build a multi-pane UI and reuse a fragment in multiple activities. You can think of a fragment as a modular section of an activity, which has its own lifecycle, receives its own input events, and which you can add or remove while the activity is running (sort of like a "sub activity" that you can reuse in different activities).</item>
        <item>Activities are one of the fundamental building blocks of apps on the Android platform. They serve as the entry point for a user's interaction with an app, and are also central to how a user navigates within an app (as with the Back button) or between apps (as with the Recents button). </item>
        <item>A Service is an application component that can perform long-running operations in the background, and it does not provide a user interface. Another application component can start a service, and it continues to run in the background even if the user switches to another application. Additionally, a component can bind to a service to interact with it and even perform interprocess communication (IPC). For example, a service can handle network transactions, play music, perform file I/O, or interact with a content provider, all from the background.</item>
        <item>Content providers can help an application manage access to data stored by itself, stored by other apps, and provide a way to share data with other apps. They encapsulate the data, and provide mechanisms for defining data security. Content providers are the standard interface that connects data in one process with code running in another process. Implementing a content provider has many advantages. </item>
    </string-array>
</resources>


communicator.java

public interface communicator {
    public void respond(int i);
}

如果int main() { int number_of_lines = 0; std::string line; std::ifstream myfile("textexample.txt"); while (std::getline(myfile, line)) ++number_of_lines; std::cout << "Number of lines in text file: " << number_of_lines; return 0; } 文件最后实际上有两个空行,那么这个程序只计算其中一个,我猜第一个。如下:

1
2
3

4
5
6


以上6个数字和3个空行总共为9行,但上面的程序将返回8。

我不知道为什么,但似乎textexample.txt只循环了8次。

1 个答案:

答案 0 :(得分:1)

示例中的文件有10行,其中3行为空。如果我运行你的代码(缺少包含...),它告诉我有10行。所以你要么运行不同的代码,要么错误引用文件。或者你的C ++标准库以某种方式被破坏了......

如果我用“结束”删除该行,我会得到9行,而不是8行。