元组元素是只读的?

tuple elements are read-only?

我正在尝试制作一个程序来扫描 HRML 标签及其属性。

#include<iostream>
#include<string>
#include<string.h>
#include<vector>
#include<algorithm>
#include<cmath>

#include<sstream>
#include<fstream>
#include<list>
#include<numeric>
#include<map>
#include<iterator>

#include<regex>
#include<climits> //#include"matrix.h"
#include<random>
#include<ctime>
#include<iomanip>

#include<set>

using namespace std; 
typedef tuple<string,vector<string>,bool> tagMap;

int main(){
    int n;
    int q;
    cin>>n>>q;
    cin.clear();
    cin.ignore(std::numeric_limits<int>::max());
    set<tagMap> TagHierarchy;
    vector<tuple<string,string,string>> tags;

    for(int i=0; i<n; ++i){
        string ln;
        getline(cin,ln);
        istringstream is{ln};
        string tagId;
        is>>tagId;
        if(find(tagId.begin(),tagId.end(),'/')!=tagId.end()){
            auto it=find_if(TagHierarchy.begin(),TagHierarchy.end(),[&](tagMap e){if(get<0>(e)==tagId)return true;return false;});
            get<2>(*it)=false; // Error is here
        }
        else{
            tagId.erase(remove(tagId.begin(),tagId.end(),'<'),tagId.end());
            for(auto& e:TagHierarchy){
                if(get<2>(e)){//active
                    vector<string> v=get<1>(e);
                    v.push_back(tagId);
                }
                else{//inactive

                }
            }
            TagHierarchy.insert(make_tuple(tagId,vector<string>{},true));
            string tagName;
            is>>tagName;
            char t;
            is>>t;
            string tagValue;
            is>>tagValue;
            tagValue.erase(remove(tagValue.begin(),tagValue.end(),'>'),tagValue.end());
            tags.push_back(make_tuple(tagId,tagName,tagValue));
        }

    }


}

问题出在第 45 行,我在那里扫描将标签更改为关闭,它给了我以下内容

error: assignment of read-only location 'std::get<2u, {std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > >, bool}>((* & it.std::_Rb_tree_const_iterator<_Tp>::operator*<std::tuple<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::vector<std::__cxx11::basi|

但是元组不是常量? 我也尝试过 for-ranged 循环,对于每个循环,但它们都给我同样的错误。

这编译得很好(为了以防万一,使用了你所有的包含):

#include<iostream>
#include<string>
#include<string.h>
#include<vector>
#include<algorithm>
#include<cmath>

#include<sstream>
#include<fstream>
#include<list>
#include<numeric>
#include<map>
#include<iterator>

#include<regex>
#include<climits> //#include"matrix.h"
#include<random>
#include<ctime>
#include<iomanip>

#include<set>

main() {
        std::tuple<std::string,std::string,std::string> t;

        std::get<0>(t) = "123";
        std::get<1>(t) = "234";
        std::get<2>(t) = "345";
}

tuple elements are read-only?

一般不会。但是 const 元组的元素是只读的。

But the tuple isn't const?

集合的元素是常量。元组是集合的一个元素。是的,它是常量。 (或者至少访问是通过常量引用;我不确定是否可以保证对象本身的常量性)