【学习日志】2022.10.16 万用头文件 sstream C++进制转换

万用头文件

 #include<bits/stdc++.h>包含了目前c++所包含的所有头文件!!!!

#include <bits/stdc++.h>

C++常用输入输出

(1条消息) C++:cin、cin.getline()、getline()的用法_lightmare625的博客-CSDN博客_cin.get和cin.getlinehttps://blog.csdn.net/weixin_41042404/article/details/80934191?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522166590681116781432913035%2522%252C%2522scm%2522%253A%252220140713.130102334..%2522%257D&request_id=166590681116781432913035&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~top_positive~default-1-80934191-null-null.142%5Ev56%5Econtrol,201%5Ev3%5Eadd_ask&utm_term=getline&spm=1018.2226.3001.4187

94ab228372df415281bd498652222348.png

#include<iostream>
using namespace std;
int main(){
    int n,num;
    while(1){
        cin>>n;
        int sum = 0;
        if(n==0){break;}
        for(int i = 0;i<n;i++){
            cin>>num;
            sum+=num;
        }
        cout<<sum<<endl;
    }
}

b3cf445944d448139fdf6304cfab22ab.png

 

#include<iostream>
using namespace std;
int main(){
    int m,n,num,sum;
    cin>>m;
    while(m--){
        cin>>n;
        sum = 0;
        while(n--){
            cin>>num;
            sum+=num;
        }
        cout<<sum<<endl;
    }
}

e1ccf8f503124b29b8934b06672ac311.png

#include<iostream>
using namespace std;
int main(){
    int n,num;
    while(cin>>n){
        int sum = 0;
        while(n--){
            cin>>num;
            sum+=num;
        }
        cout<<sum<<endl;
    }
}

aa74013db95242e2a85616437a4c9f8d.png

#include <iostream>
using namespace std;
int main(){
    int sum,num;
    while(cin>>num){
        sum+=num;
        if(cin.get()=='\n'){
            cout<<sum<<endl;
            sum=0;
        }
    }
}

 0e8ab6bd927249a59b126ba4c6e62c5a.png

#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
using namespace std;
#define rep(i,a,n) for(int i=a;i<n;i++)
int main(){
    int n;
    cin>>n;
    vector<string> v(n);
    rep(i,0,n){
        string s;
        cin>>s;
        v[i]=s;
    }
    sort(v.begin(),v.end());
    rep(i,0,n){
        cout<<v[i]<<" ";
    }
}

 b1fcd8f5d8b64438ad828681ea1965f4.png

#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;
#define rep(i,a,n) for(int i=a;i<n;i++)
int main(){
    vector<string> v;
    string s;
    while(cin>>s){
        v.push_back(s);
        if(cin.get()=='\n'){
            sort(v.begin(),v.end());
            for(auto i:v){
                cout<<i<<' ';
            }
            cout<<endl;
            v.clear();
        }
    }
}

2559a29e2a6c4ab384c0e675c8d9af07.png

#include <bits/stdc++.h>
using namespace std;
 
int main(){
    string s;
    while (cin >> s){
        vector<string> v;
        string wd;
         
        stringstream ss;
        ss << s;
         
        while (getline(ss,wd,','))
            v.push_back(wd);
 
        std::sort(v.begin(), v.end());
         
        for (int i = 0; i < v.size() - 1; i ++ )
            cout << v[i] << ',';
        cout << v[v.size() - 1] << endl;
    }
    return 0;
}

 


sstream

 <sstream>库定义了三种类:istringstream、ostringstream和stringstream,分别用来进行流的输入、输出和输入输出操作。

stringstream

(2条消息) C++ stringstream 简单使用_原来是枫哥呀!的博客-CSDN博客_c++ stringstreamhttps://blog.csdn.net/weixin_45867382/article/details/122109133?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522166590209016782248519371%2522%252C%2522scm%2522%253A%252220140713.130102334..%2522%257D&request_id=166590209016782248519371&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~sobaiduend~default-2-122109133-null-null.142%5Ev56%5Econtrol,201%5Ev3%5Eadd_ask&utm_term=%20stringstream%20&spm=1018.2226.3001.4187

8e1835891fa54d3f865bad817ddb7838.png

<<不会覆盖,ss(str)在<<添加时会被覆盖掉

实现任意类型的转换

template<typename out_type, typename in_value>
    out_type convert(const in_value & t){
      stringstream stream;
      stream<<t;//向流中传值
      out_type result;//这里存储转换结果
      stream>>result;//向result中写入值
      return result;
    }
int main(){
    string s = "1 23 # 4";
    stringstream ss;
    ss<<s;
    while(ss>>s){
        cout<<s<<endl;
        int val = convert<int>(s);
        cout<<val<<endl;
    }
    return 0;
}

sprintf() 

int main(){
    char str[256] = { 0 };
    int data = 1024;
    //将data转换为字符串
    sprintf(str,"%d",data);
    //获取data的十六进制
    sprintf(str,"0x%X",data);
    //获取data的八进制
    sprintf(str,"0%o",data);
    const char *s1 = "Hello";
    const char *s2 = "World";
    //连接字符串s1和s2
    sprintf(str,"%s %s",s1,s2);
    cout<<str<<endl; 
    return 0;
} 

getline用法总结

// getlineDemo.cpp : 定义控制台应用程序的入口点。
//
 
#include "stdafx.h"
#include <iostream>
#include <sstream>
#include <string>
 
#include <fstream>
 
/*
 *	Note:
    这里需要区分 getline和 std::cin.getline,是属不同的二个类中的函数,行参不一样,但功能是差不多的
    一, getline(cin,str)是string类对象的成员函数,即string::getline,使用时需头文件#include <string>,例子:1,2,4
     declaration:
     (1) istream& getline (istream& is, string& str, char delim);
     (2) istream& getline (istream& is, string& str);
    二, cin.getline是输入流对象的成员函数,即istream::getline,头文件#include<iostream> ,例子:3
     cin.getline
     istream& getline (char* s, streamsize n );
     istream& getline (char* s, streamsize n, char delim );
 */
 
 
const char* g_fileName = "file.txt";
bool creatFile()
{
    std::ofstream ofstr( g_fileName );
    if ( !ofstr )
       return false;
 
    std::stringstream ss;
    ss<<"this is the first line \n";
    ss<<"this is the second line \n";
    ss<<"this is the third line \n";
    
    ofstr << ss.str();
    ofstr.close();
 
    return true;
}
 
int _tmain(int argc, _TCHAR* argv[])
{
    // 1, 读取一个流串到string中,设置停止条件
    std::stringstream ss;
    ss << "abcdefgh";
    std::string tempStr = "";
    getline( ss, tempStr, 'd' ); // or like this: getline( std::cin, tempStr, 'd' )
    std::cout << "处理前的字符串:" << ss.str() << ", getline处理遇'd'之后的字符串:" << tempStr << std::endl;
 
    // 2, 读取一个流串到string中,没有设置停止条件
    ss.seekg( std::ios_base::beg ); // or: ss.seekg(0, ss.beg );
    tempStr.clear();
    getline( ss, tempStr ); // or like this: getline( std::cin, tempStr, 'd' )
    std::cout << "处理前的字符串:" << ss.str() << ", getline读取整行的字符串:" << tempStr << std::endl;
 
    // 3,读取一个流串到char数组
    ss.seekg( std::ios_base::beg );
    char charArr[16] = {0};
    std::cout << "请输入一个中间带'd'的字符串:";
    std::cin.getline( charArr, 16, 'd' );
    std::cout << "通过cin.getline的方式过滤带'd'的字符串之后的内容:" << charArr << std::endl;
    
    // 4,getline循环读取文件中的每一行内容
    if ( !creatFile() ) 
        return -1;
 
    std::ifstream ifstr( g_fileName );
    if ( !ifstr )
        return -1;
 
    unsigned int lineNum = 1;
    while( getline( ifstr, tempStr ))
    {
        std::cout << " LineNum: " << lineNum << " , contens: " << tempStr << std::endl;
        ++lineNum;
        tempStr.clear();
    }
 
    ifstr.close();
    system("pause");
 
	return 0;
}

C++string类常用方法

使用 string.insert() 进行插入操作

string& insert(size_t pos,const string&str);   
// 在位置 pos 处插入字符串 str

string& insert(size_t pos,const string&str,size_t subpos,size_t sublen); 
// 在位置 pos 处插入字符串 str 的从位置 subpos 处开始的 sublen 个字符

string& insert(size_t pos,const char * s);    
// 在位置 pos 处插入字符串 s

string& insert(size_t pos,const char * s,size_t n); 
// 在位置 pos 处插入字符串 s 的前 n 个字符

string& insert(size_t pos,size_t n,char c);      
// 在位置 pos 处插入 n 个字符 c

iterator insert (const_iterator p, size_t n, char c); 
// 在 p 处插入 n 个字符 c,并返回插入后迭代器的位置

iterator insert (const_iterator p, char c);       
// 在 p 处插入字符 c,并返回插入后迭代器的位置

261f4e77e04b47cc84e96762e14c0c5d.png

使用 string.erase() 进行元素删除操作

string& erase (size_t pos = 0, size_t len = npos);   // 删除从 pos 处开始的 n 个字符
iterator erase (const_iterator p);            // 删除 p 处的一个字符,并返回删除后迭代器的位置
iterator erase (const_iterator first, const_iterator last); // 删除从 first 到last 之间的字符,并返回删除后迭代器的位置

使用 getline() 函数来获取 string 输入

string str;
getline(cin, str);

ListNode的结构

struct ListNode {
      int val;  //当前结点的值
      ListNode *next;  //指向下一个结点的指针
      ListNode(int x) : val(x), next(NULL) {}  //初始化当前结点值为x,指针为空
  };

如何向ListNode中插入新的结点:从键盘输入

ListNode* temp1 = new Solution::ListNode(0); //创建新元素,
ListNode* l1 = temp1; //最后的结果l1指向temp1,这样可以获取temp所接收的全部元素,而temp的指针由于每次都往下移,所以每次都更新

while ((c = getchar()) != '\n')   //以空格区分各个结点的值
        {
            if (c != ' ')
            {
                ungetc(c, stdin);  //把不是空格的字符丢回去
                cin >> num;
                Solution::ListNode* newnode = new Solution::ListNode(0);
                newnode->val = num;//创建新的结点存放键盘中读入的值
                newnode->next = NULL;
                temp2->next = newnode;//并将其赋值给temp2
                temp2 = newnode; //此处也可以写成  temp2=temp2->next,使指针指向下一个,以待接收新元素
            }
        }
逆序输出所有元素

void  Solution::reversePrintListNode(ListNode* head)
{
    if (head == nullptr) return;
     cout << head->val; //顺序输出
    reversePrintListNode(head->next);  
    cout << head->val; //逆序输出
   
}

十进制转二进制 

#include<bits/stdc++.h>
using namespace std;
int n,ans;//ans储存二进制位数,方便输出
vector<int> a;//因为n的范围是1e9,所以我定了一个动态数组
int main(){
cin>>n;
while(n>0){
ans++;//存位数
a.push_back(n%2);//如数组
n/=2;}
for(int i=ans-1;i>=0;i--) cout<<a[i];}//倒序输出

 

 

 

 

 

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值