c++ 常见操作

1、vector常见操作

1、vector声明

vector a; 

2、vector插入值

//在vector尾部加上元素v
a.push_back(v)

//在某一位置加入某一元素;在a的第5个位置加入元素100
a.insert(a.begin()+5,100);

3、vector遍历

    //以数组形式遍历vector
    cout<<"数组方式遍历开始:";
    for(int i=0;i::iterator it=a.begin();it!=a.end();it++){
        cout<<*it<<",";
    }

4、vector删除元素

//pop_back删除最后一个数,返回为空
a.pop_back(); 

//删除迭代器指向的元素,删除第5个元素
a.erase( a.begin()+5 );

//删除所有元素
a.erase(a.begin(),a.end());
a.clear();

5、判断某一元素是否在vector中

vector v{ 4, 7, 9, 1, 2, 5 };
int key = 2;
//count判断 如果count为0,表示该元素不存在于v中
if (count(v.begin(), v.end(), key))

//find判断 find(v.begin(), v.end(), key)!= v.end()表示该元素存在于v中
if (std::find(v.begin(), v.end(), key) != v.end())

2、字符串和数值类型相互转换

1、数值类型转化为字符串类型

//to_string实现数值类型转化为字符串类型
std::to_string(3.1415926)

//std::ostringstream字符串转化
std::ostringstream stream;

stream<

2、字符串类型转化为数值类型

std::string str;
int i = atoi(str.c_str());

//其他数值类型的转化stol(long), stof(float), stod(double) 

3、数值类型的vector转化为字符串

vector vec = {1,2,3,4};
stringstream ss;
string str;
copy(vec.begin(), vec.end(), ostream_iterator(ss, ""));
str = ss.str();

4、字符串转化为数值类型的vector

void split(const std::string &delimiters, const std::string &source,
               std::vector &result) {
        size_t prev_pos = 0, pos = 0;
        result.resize(0);
        pos = source.find_first_of(delimiters, pos);
        while (pos != std::string::npos) {
            result.push_back(source.substr(prev_pos, pos - prev_pos));
            prev_pos = ++pos;
            pos = source.find_first_of(delimiters, pos);
        }
        if (prev_pos < source.size()) {
            result.push_back(source.substr(prev_pos));
        }
    }
    void split(char delimiter, const std::string &source,
               std::vector &result) {
        size_t prev_pos = 0, pos = 0;
        result.resize(0);
        pos = source.find(delimiter, pos);
        while (pos != std::string::npos) {
            result.push_back(source.substr(prev_pos, pos - prev_pos));
            prev_pos = ++pos;
            pos = source.find(delimiter, pos);
        }
        if (prev_pos < source.size()) {
            result.push_back(source.substr(prev_pos));
        }
    }
    const std::vector split(char delimiter, const std::string &source) {
        size_t prev_pos = 0, pos = 0;
        std::vector result;
        result.reserve(std::count(source.begin(), source.end(), delimiter) + 1);
        pos = source.find(delimiter, pos);
        while (pos != std::string::npos) {
            result.push_back(source.substr(prev_pos, pos - prev_pos));
            prev_pos = ++pos;
            pos = source.find(delimiter, pos);
        }
        if (prev_pos < source.size()) {
            result.push_back(source.substr(prev_pos));
        }
        return result;
    }
    



【C++】int转换为string的两种方法(to_string、字符串流)_chavo0的博客-CSDN博客_c++ int转换为string

C++ vector<int> 转 string_Pisces_224的博客-CSDN博客_c++ vector转string

c++判断vector中是否存在特定元素的方法_guotianqing的博客-CSDN博客_vector判断元素是否存在

c++之vector常见操作_frostjsy的博客-CSDN博客_c++vector操作

c++常见map操作_frostjsy的博客-CSDN博客

C++ - string类型转换int类型_SpikeKing的博客-CSDN博客_cpp string 转int

你可能感兴趣的:(c++,c++,蓝桥杯,拓扑学)