for循环

#include <iostream>
using namespace std;

int main(int argc, char const *argv[])
{
    int sum=0;
    for (int i = 50; i <= 100; i++)
    {
        sum+=i;
    }
    cout<<"50到100(包括50和100)的书累加和为:"<<sum<<endl; 
    //50到100(包括50和100)的书累加和为:3825
    return 0;
}

C++11的for range, 用g++编译的时候必须指明为c++11标准:clang++ -std=c++11 -g -o main a.cpp

#include <iostream>
using namespace std;

int main(int argc, char const *argv[])
{
    int arr[] = {1,2,3,4,5};
    for(auto i:arr)
        cout<<i<<" ";
    cout<<endl;
    return 0;
}
#include <iostream>
#include <string>
using namespace std;

int main(int argc, char const *argv[])
{
    string s;
    cout<<"请输入一个字符串"<<endl;
    getline(cin,s);//string头文件 提供了2个2歌方法读取用户输入
    //getline(cin,s)读取句子,遇到\0终止
    //cin>>s; 读取word,即单词,遇空格终止
    for(auto &c:s)//auto 编译器自动推断变量类型 这里必须传引用才可以修改
        c='x';
    cout<<s<<endl;
    return 0;
}
请输入一个字符串
Regarding the VAL and MG Mockup Guides updates for 2021.
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

results matching ""

    No results matching ""